@@ -37,6 +37,38 @@ int main(void) {
3737 exit (1 );
3838 }
3939 }
40+
41+ // ensure short argument precedes long argument
42+ if (opt.args .size () > 1 ) {
43+ const std::string first (opt.args .front ());
44+ const std::string last (opt.args .back ());
45+
46+ // only check if we have a mix of short (-x) and long (--xxx) forms
47+ bool first_is_short = (first.size () >= 2 && first[0 ] == ' -' && first[1 ] != ' -' );
48+ bool last_is_short = (last.size () >= 2 && last[0 ] == ' -' && last[1 ] != ' -' );
49+
50+ // if at least one is short, verify order
51+ if ((first_is_short || last_is_short) && first.length () > last.length ()) {
52+ fprintf (stderr, " test-arg-parser: short form should come before long form: %s, %s\n " ,
53+ first.c_str (), last.c_str ());
54+ assert (false );
55+ }
56+ }
57+
58+ // same check for negated arguments
59+ if (opt.args_neg .size () > 1 ) {
60+ const std::string first (opt.args_neg .front ());
61+ const std::string last (opt.args_neg .back ());
62+
63+ bool first_is_short = (first.size () >= 2 && first[0 ] == ' -' && first[1 ] != ' -' );
64+ bool last_is_short = (last.size () >= 2 && last[0 ] == ' -' && last[1 ] != ' -' );
65+
66+ if ((first_is_short || last_is_short) && first.length () > last.length ()) {
67+ fprintf (stderr, " test-arg-parser: short negated form should come before long form: %s, %s\n " ,
68+ first.c_str (), last.c_str ());
69+ assert (false );
70+ }
71+ }
4072 }
4173 } catch (std::exception & e) {
4274 printf (" %s\n " , e.what ());
0 commit comments