Skip to content

Commit 8987951

Browse files
committed
Fix help behavior by intrducing child argparser
1 parent ee2da6d commit 8987951

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

source-code/application/my_app.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def generate_data(n, a=0, b=1):
1111
return [random.randint(a, b) for _ in range(n)]
1212

1313
def main():
14-
arg_parser = ArgumentParser(description='test application')
14+
arg_parser = ArgumentParser(add_help=False)
1515
arg_parser.add_argument('--conf',
1616
help='configuration file to use')
1717
arg_parser.add_argument('--verbose', action='store_true',
@@ -36,12 +36,14 @@ def main():
3636
print('user configuration file values:', file=sys.stderr)
3737
cfg.write(sys.stderr)
3838
cfg_opts = dict(cfg['defaults'])
39-
arg_parser.set_defaults(**cfg_opts)
40-
arg_parser.add_argument('n', type=int, nargs='?',
41-
help='number of random values to generate')
42-
arg_parser.add_argument('--a', type=int, help='smallest value')
43-
arg_parser.add_argument('--b', type=int, help='largest value')
44-
arg_parser.parse_args(remaining_options, options)
39+
arg_parser_cl = ArgumentParser(description='test application',
40+
parents=[arg_parser])
41+
arg_parser_cl.set_defaults(**cfg_opts)
42+
arg_parser_cl.add_argument('n', type=int, nargs='?',
43+
help='number of random values to generate')
44+
arg_parser_cl.add_argument('--a', type=int, help='smallest value')
45+
arg_parser_cl.add_argument('--b', type=int, help='largest value')
46+
arg_parser_cl.parse_args(remaining_options, options)
4547
if options.verbose:
4648
print('final options:', file=sys.stderr)
4749
print(f'n = {options.n}\na = {options.a}\nb = {options.b}', end='\n\n',

0 commit comments

Comments
 (0)