MCPcopy Create free account
hub / github.com/ElementsProject/lightning / parse_one

Function parse_one

ccan/ccan/opt/parse.c:32–143  ·  view source on GitHub ↗

Returns 1 if argument consumed, 0 if all done, -1 on error. */

Source from the content-addressed store, hash-verified

30
31/* Returns 1 if argument consumed, 0 if all done, -1 on error. */
32int parse_one(int *argc, char *argv[], enum opt_type is_early, unsigned *offset,
33 void (*errlog)(const char *fmt, ...), bool unknown_ok)
34{
35 unsigned i, arg, len;
36 const char *o, *optarg = NULL;
37 char *problem = NULL;
38
39 if (getenv("POSIXLY_CORRECT")) {
40 /* Don't find options after non-options. */
41 arg = 1;
42 } else {
43 for (arg = 1; argv[arg]; arg++) {
44 if (argv[arg][0] == '-')
45 break;
46 }
47 }
48
49 if (!argv[arg] || argv[arg][0] != '-')
50 return 0;
51
52 /* Special arg terminator option. */
53 if (strcmp(argv[arg], "--") == 0) {
54 consume_option(argc, argv, arg);
55 return 0;
56 }
57
58 /* Long options start with -- */
59 if (argv[arg][1] == '-') {
60 assert(*offset == 0);
61 for (o = first_lopt(&i, &len); o; o = next_lopt(o, &i, &len)) {
62 if (strncmp(argv[arg] + 2, o, len) != 0)
63 continue;
64 if (argv[arg][2 + len] == '=')
65 optarg = argv[arg] + 2 + len + 1;
66 else if (argv[arg][2 + len] != '\0')
67 continue;
68 break;
69 }
70 if (!o) {
71 if (unknown_ok)
72 goto ok;
73 return parse_err(errlog, argv[0],
74 argv[arg], strlen(argv[arg]),
75 "unrecognized option");
76 }
77 /* For error messages, we include the leading '--' */
78 o -= 2;
79 len += 2;
80 } else {
81 /* offset allows us to handle -abc */
82 for (o = first_sopt(&i); o; o = next_sopt(o, &i)) {
83 if (argv[arg][*offset + 1] != *o)
84 continue;
85 (*offset)++;
86 break;
87 }
88 if (!o) {
89 if (unknown_ok) {

Callers 2

opt_parseFunction · 0.85
early_parseFunction · 0.85

Calls 6

consume_optionFunction · 0.85
first_loptFunction · 0.85
next_loptFunction · 0.85
parse_errFunction · 0.85
first_soptFunction · 0.85
next_soptFunction · 0.85

Tested by

no test coverage detected