GetSysInfo.cpp 1.6 KB
Newer Older
J
jinhai 已提交
1
#include <iostream>
J
jinhai 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#include <getopt.h>
#include <memory.h>

// Not provide path: current work path will be used and system.info.

void
print_usage(const std::string &app_name) {
    printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str());
    printf("  Options:\n");
    printf("   -h --help               Print this help\n");
    printf("   -d --sysinfo filename   Generate system info file as given name\n");
    printf("\n");
}
J
jinhai 已提交
15 16

int main(int argc, char* argv[]) {
J
jinhai 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    std::string app_name = argv[0];
    if(argc != 1 && argc != 3) {
        print_usage(app_name);
        return EXIT_FAILURE;
    }

    static struct option long_options[] = {{"system_info", required_argument, 0, 'd'},
                                           {"help", no_argument, 0, 'h'},
                                           {NULL, 0, 0, 0}};
    int value = 0;
    int option_index = 0;
    std::string system_info_filename = "./system.info";
    while ((value = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) {
        switch (value) {
            case 'd': {
                char *system_info_filename_ptr = strdup(optarg);
                system_info_filename = system_info_filename_ptr;
                free(system_info_filename_ptr);
//                printf("Generate system info file: %s\n", system_info_filename.c_str());
                break;
            }
            case 'h':
                print_usage(app_name);
                return EXIT_SUCCESS;
            case '?':
                print_usage(app_name);
                return EXIT_FAILURE;
            default:
                print_usage(app_name);
                break;
        }
    }

J
jinhai 已提交
50 51
    return 0;
}