Skip to content

Commit a60edfb

Browse files
committed
parse: provide flags for parsing
1 parent 4077f1c commit a60edfb

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Types of options
2828
* Literal separators are bare double-dashes, `--` as a lone
2929
word, indicating that further words will be treated as
3030
arguments (even if they begin with a dash).
31-
* Arguments are options that are bare words, not prefixed with
32-
a single or double dash, for example `filename.txt`.
31+
* Arguments are bare words, not prefixed with a single or double dash,
32+
for example `filename.txt`.
3333
* Argument lists are the remainder of arguments, not prefixed
3434
with dashes. For example, an array: `file1.txt`, `file2.txt`,
3535
...
@@ -99,7 +99,7 @@ int main(int argc, const char **argv)
9999
const char *value;
100100
const char *file;
101101

102-
if (adopt_parse(&result, opt_specs, argv + 1, argc - 1) < 0) {
102+
if (adopt_parse(&result, opt_specs, argv + 1, argc - 1, ADOPT_PARSE_DEFAULT) < 0) {
103103
adopt_status_fprint(stderr, &opt);
104104
adopt_usage_fprint(stderr, argv[0], opt_specs);
105105
return 129;

adopt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ adopt_status_t adopt_parse(
354354
adopt_opt *opt,
355355
const adopt_spec specs[],
356356
char **args,
357-
size_t args_len)
357+
size_t args_len,
358+
unsigned int flags)
358359
{
359360
adopt_parser parser;
360361
const adopt_spec **given_specs;

adopt.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ typedef enum {
8282
ADOPT_USAGE_SHOW_LONG = (1u << 5),
8383
} adopt_usage_t;
8484

85+
typedef enum {
86+
/** Default parsing behavior. */
87+
ADOPT_PARSE_DEFAULT = 0,
88+
} adopt_flag_t;
89+
8590
/** Specification for an available option. */
8691
typedef struct adopt_spec {
8792
/** Type of option expected. */
@@ -223,12 +228,14 @@ typedef struct adopt_parser {
223228
* @param specs A NULL-terminated array of `adopt_spec`s that can be parsed
224229
* @param args The arguments that will be parsed
225230
* @param args_len The length of arguments to be parsed
231+
* @param flags The flags for parsing
226232
*/
227233
adopt_status_t adopt_parse(
228234
adopt_opt *opt,
229235
const adopt_spec specs[],
230236
char **args,
231-
size_t args_len);
237+
size_t args_len,
238+
unsigned int flags);
232239

233240
/**
234241
* Initializes a parser that parses the given arguments according to the

examples/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
5555
adopt_opt result;
5656
size_t i;
5757

58-
if (adopt_parse(&result, opt_specs, argv + 1, argc - 1) != 0) {
58+
if (adopt_parse(&result, opt_specs, argv + 1, argc - 1, ADOPT_PARSE_DEFAULT) != 0) {
5959
adopt_status_fprint(stderr, argv[0], &result);
6060
adopt_usage_fprint(stderr, argv[0], opt_specs);
6161
return 129;

tests/adopt.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ void test_adopt__parse_oneshot(void)
602602

603603
char *args[] = { "-f", "--bar" };
604604

605-
cl_must_pass(adopt_parse(&result, specs, args, 2));
605+
cl_must_pass(adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
606606

607607
cl_assert_equal_i('f', foo);
608608
cl_assert_equal_i('b', bar);
@@ -625,7 +625,7 @@ void test_adopt__parse_oneshot_unknown_option(void)
625625

626626
char *args[] = { "-f", "--bar", "--asdf" };
627627

628-
cl_assert_equal_i(ADOPT_STATUS_UNKNOWN_OPTION, adopt_parse(&result, specs, args, 3));
628+
cl_assert_equal_i(ADOPT_STATUS_UNKNOWN_OPTION, adopt_parse(&result, specs, args, 3, ADOPT_PARSE_DEFAULT));
629629
}
630630

631631
void test_adopt__parse_oneshot_missing_value(void)
@@ -642,7 +642,7 @@ void test_adopt__parse_oneshot_missing_value(void)
642642

643643
char *args[] = { "-f", "--bar" };
644644

645-
cl_assert_equal_i(ADOPT_STATUS_MISSING_VALUE, adopt_parse(&result, specs, args, 2));
645+
cl_assert_equal_i(ADOPT_STATUS_MISSING_VALUE, adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
646646
}
647647

648648
void test_adopt__parse_arg(void)
@@ -661,7 +661,7 @@ void test_adopt__parse_arg(void)
661661

662662
char *args[] = { "-f", "bar", "baz" };
663663

664-
cl_must_pass(adopt_parse(&result, specs, args, 3));
664+
cl_must_pass(adopt_parse(&result, specs, args, 3, ADOPT_PARSE_DEFAULT));
665665

666666
cl_assert_equal_i('f', foo);
667667
cl_assert_equal_p(NULL, bar);
@@ -689,7 +689,7 @@ void test_adopt__parse_arg_mixed_with_switches(void)
689689

690690
char *args[] = { "-f", "bar", "baz", "--bar" };
691691

692-
cl_must_pass(adopt_parse(&result, specs, args, 4));
692+
cl_must_pass(adopt_parse(&result, specs, args, 4, ADOPT_PARSE_DEFAULT));
693693

694694
cl_assert_equal_i('f', foo);
695695
cl_assert_equal_p('b', bar);
@@ -718,7 +718,7 @@ void test_adopt__parse_arg_with_literal(void)
718718

719719
char *args[] = { "-f", "--", "--bar" };
720720

721-
cl_must_pass(adopt_parse(&result, specs, args, 3));
721+
cl_must_pass(adopt_parse(&result, specs, args, 3, ADOPT_PARSE_DEFAULT));
722722

723723
cl_assert_equal_i('f', foo);
724724
cl_assert_equal_p(NULL, bar);
@@ -745,7 +745,7 @@ void test_adopt__parse_args(void)
745745

746746
char *args[] = { "-f", "--bar", "BRR", "one", "two", "three", "four" };
747747

748-
cl_must_pass(adopt_parse(&result, specs, args, 7));
748+
cl_must_pass(adopt_parse(&result, specs, args, 7, ADOPT_PARSE_DEFAULT));
749749

750750
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
751751
cl_assert_equal_p(NULL, result.arg);
@@ -777,7 +777,7 @@ void test_adopt__parse_args_with_literal(void)
777777

778778
char *args[] = { "-f", "--", "--bar", "asdf", "--baz" };
779779

780-
cl_must_pass(adopt_parse(&result, specs, args, 5));
780+
cl_must_pass(adopt_parse(&result, specs, args, 5, ADOPT_PARSE_DEFAULT));
781781

782782
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
783783
cl_assert_equal_p(NULL, result.arg);
@@ -807,7 +807,7 @@ void test_adopt__parse_args_implies_literal(void)
807807

808808
char *args[] = { "-f", "foo", "bar", "--bar" };
809809

810-
cl_must_pass(adopt_parse(&result, specs, args, 4));
810+
cl_must_pass(adopt_parse(&result, specs, args, 4, ADOPT_PARSE_DEFAULT));
811811

812812
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
813813
cl_assert_equal_p(NULL, result.arg);
@@ -837,7 +837,7 @@ void test_adopt__value_required(void)
837837

838838
char *args[] = { "-f", "--bar" };
839839

840-
cl_must_pass(adopt_parse(&result, specs, args, 2));
840+
cl_must_pass(adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
841841

842842
cl_assert_equal_i(ADOPT_STATUS_MISSING_VALUE, result.status);
843843
}
@@ -857,7 +857,7 @@ void test_adopt__required_choice_missing(void)
857857

858858
char *args[] = { "foo", "bar" };
859859

860-
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, adopt_parse(&result, specs, args, 2));
860+
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
861861

862862
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, result.status);
863863
cl_assert_equal_s("foo", result.spec->name);
@@ -883,7 +883,7 @@ void test_adopt__required_choice_specified(void)
883883

884884
char *args[] = { "--bar" };
885885

886-
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, adopt_parse(&result, specs, args, 2));
886+
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
887887

888888
cl_assert_equal_i(ADOPT_STATUS_MISSING_ARGUMENT, result.status);
889889
cl_assert_equal_s("baz", result.spec->name);
@@ -910,7 +910,7 @@ void test_adopt__choice_switch_or_arg_advances_arg(void)
910910

911911
char *args[] = { "-z", "actually_final" };
912912

913-
cl_assert_equal_i(ADOPT_STATUS_DONE, adopt_parse(&result, specs, args, 2));
913+
cl_assert_equal_i(ADOPT_STATUS_DONE, adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
914914

915915
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
916916
cl_assert_equal_p(NULL, result.arg);
@@ -938,7 +938,7 @@ void test_adopt__stop(void)
938938

939939
char *args[] = { "-f", "--help", "-z" };
940940

941-
cl_assert_equal_i(ADOPT_STATUS_DONE, adopt_parse(&result, specs, args, 2));
941+
cl_assert_equal_i(ADOPT_STATUS_DONE, adopt_parse(&result, specs, args, 2, ADOPT_PARSE_DEFAULT));
942942

943943
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
944944
cl_assert_equal_s("--help", result.arg);

0 commit comments

Comments
 (0)