-
- import argparse
-
-
- def makep():
- # Global options
- options = argparse.ArgumentParser(add_help=False)
- options.add_argument('-v', '--verbose', action='store_true')
-
- # Main parser, accepts general options
- parser = argparse.ArgumentParser(parents=[options])
-
- # Subparser, accepts general options
- subparsers = parser.add_subparsers()
- parser_cmd = subparsers.add_parser('cmd', parents=[options])
-
- return parser
-
-
- def main():
- assert not makep().parse_args('cmd'.split()).verbose
- assert makep().parse_args('cmd -v'.split()).verbose
- assert makep().parse_args('-v cmd -v'.split()).verbose
- assert makep().parse_args('-v cmd'.split()).verbose # fails
-
-
- if __name__ == '__main__':
- main()
-