static int parse_argv(int argc, char *argv[]) { enum { ARG_LOG_LEVEL = 0x100, ARG_LOG_TARGET, ARG_LOG_COLOR, ARG_LOG_LOCATION, ARG_EXIT_CODE, }; static const struct option options[] = { { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, { "log-target", required_argument, NULL, ARG_LOG_TARGET }, { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, { "exit-code", required_argument, NULL, ARG_EXIT_CODE }, {} }; int c, r; assert(argc >= 1); assert(argv); /* "-" prevents getopt from permuting argv[] and moving the verb away * from argv[1]. Our interface to initrd promises it'll be there. */ while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0) switch (c) { case ARG_LOG_LEVEL: r = log_set_max_level_from_string(optarg); if (r < 0) log_error("Failed to parse log level %s, ignoring.", optarg); break; case ARG_LOG_TARGET: r = log_set_target_from_string(optarg); if (r < 0) log_error("Failed to parse log target %s, ignoring", optarg); break; case ARG_LOG_COLOR: if (optarg) { r = log_show_color_from_string(optarg); if (r < 0) log_error("Failed to parse log color setting %s, ignoring", optarg); } else log_show_color(true); break; case ARG_LOG_LOCATION: if (optarg) { r = log_show_location_from_string(optarg); if (r < 0) log_error("Failed to parse log location setting %s, ignoring", optarg); } else log_show_location(true); break; case ARG_EXIT_CODE: r = safe_atou8(optarg, &arg_exit_code); if (r < 0) log_error("Failed to parse exit code %s, ignoring", optarg); break; case '\001': if (!arg_verb) arg_verb = optarg; else log_error("Excess arguments, ignoring"); break; case '?': return -EINVAL; default: assert_not_reached("Unhandled option code."); } if (!arg_verb) { log_error("Verb argument missing."); return -EINVAL; } return 0; }
static int parse_argv(int argc, char *argv[]) { enum { ARG_LOG_LEVEL = 0x100, ARG_LOG_TARGET, ARG_LOG_COLOR, ARG_LOG_LOCATION, }; static const struct option options[] = { { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, { "log-target", required_argument, NULL, ARG_LOG_TARGET }, { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, {} }; int c, r; assert(argc >= 1); assert(argv); while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) switch (c) { case ARG_LOG_LEVEL: r = log_set_max_level_from_string(optarg); if (r < 0) log_error("Failed to parse log level %s, ignoring.", optarg); break; case ARG_LOG_TARGET: r = log_set_target_from_string(optarg); if (r < 0) log_error("Failed to parse log target %s, ignoring", optarg); break; case ARG_LOG_COLOR: if (optarg) { r = log_show_color_from_string(optarg); if (r < 0) log_error("Failed to parse log color setting %s, ignoring", optarg); } else log_show_color(true); break; case ARG_LOG_LOCATION: if (optarg) { r = log_show_location_from_string(optarg); if (r < 0) log_error("Failed to parse log location setting %s, ignoring", optarg); } else log_show_location(true); break; case '?': return -EINVAL; default: assert_not_reached("Unhandled option code."); } if (optind >= argc) { log_error("Verb argument missing."); return -EINVAL; } arg_verb = argv[optind]; if (optind + 1 < argc) log_error("Excess arguments, ignoring"); return 0; }