linux应用中获取命令行参数处理方式

/*
 *  tslib/tests/ts_print.c
 *
 *  Derived from tslib/src/ts_test.c by Douglas Lowder
 *  Just prints touchscreen events -- does not paint them on framebuffer
 *
 * This file is placed under the GPL.  Please see the file
 * COPYING for more details.
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 *
 * Basic test program for touchscreen library.
 */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>

#include "tslib.h"

static void usage(char **argv)
{
    ts_print_ascii_logo(16);
    printf("%s", tslib_version());
    printf("\n");
    printf("Usage: %s [--raw]\n", argv[0]);
    printf("\n");
    printf("-r --raw\n");
    printf("                don't apply filter modules. use what module_raw\n");
    printf("                delivers directly. This is equivalent to\n");
    printf("                running the ts_print_raw program\n");
    printf("-h --help\n");
    printf("                print this help text\n");
    printf("-v --version\n");
    printf("                print version information only\n");
}

int main(int argc, char **argv)
{
    struct tsdev *ts;
    short raw = 0;

    while (1) {
        const struct option long_options[] = {
            { "version",      no_argument,       NULL, 'v' },
            { "help",         no_argument,       NULL, 'h' },
            { "raw",          no_argument,       NULL, 'r' },
        };

        int option_index = 0;
        int c = getopt_long(argc, argv, "vrh", long_options, &option_index);

        errno = 0;
        if (c == -1)
            break;

        switch (c) {
        case 'r':
            raw = 1;
            break;

        case 'v':
            printf("%s", tslib_version());
            return 0;

        case 'h':
            usage(argv);
            return 0;

        default:
            usage(argv);
            return 0;
        }

        if (errno) {
            char str[9];

            sprintf(str, "option ?");
            str[7] = c & 0xff;
            perror(str);
        }
    }

    ts = ts_setup(NULL, 0);
    if (!ts) {
        perror("ts_setup");
        exit(1);
    }

    while (1) {
        struct ts_sample samp;
        int ret;

        if (raw)
            ret = ts_read_raw(ts, &samp, 1);
        else
            ret = ts_read(ts, &samp, 1);

        if (ret < 0) {
            perror("ts_read");
            ts_close(ts);
            exit(1);
        }

        if (ret != 1)
            continue;

        printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec, samp.x, samp.y, samp.pressure);

    }

    ts_close(ts);
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值