Skip to content

Instantly share code, notes, and snippets.

@von
Created April 30, 2011 01:57
Show Gist options
  • Select an option

  • Save von/949337 to your computer and use it in GitHub Desktop.

Select an option

Save von/949337 to your computer and use it in GitHub Desktop.

Revisions

  1. von created this gist Apr 30, 2011.
    35 changes: 35 additions & 0 deletions p-with-fixed-help.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/usr/bin/env python
    import argparse
    import ConfigParser

    conf_parser = argparse.ArgumentParser(
    # Turn off help, so we print all options in response to -h
    add_help=False
    )
    conf_parser.add_argument("-c", "--conf_file",
    help="Specify config file", metavar="FILE")
    args, remaining_argv = conf_parser.parse_known_args()
    defaults = {
    "option1" : "some default",
    "option2" : "some other default",
    }
    if args.conf_file:
    config = ConfigParser.SafeConfigParser()
    config.read([args.conf_file])
    defaults = dict(config.items("Defaults"))

    # Don't surpress add_help here so it will handle -h
    parser = argparse.ArgumentParser(
    # Inherit options from config_parser
    parents=[conf_parser],
    # print script description with -h/--help
    description=__doc__,
    # Don't mess with format of description
    formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.set_defaults(**defaults)
    parser.add_argument("--option1", help="some option")
    parser.add_argument("--option2", help="some other option")
    args = parser.parse_args(remaining_argv)
    print args