我刚刚发现您可以使用
argparse.ArgumentParser.parse_known_args()。首先使用
parse_known_args()从命令行解析配置文件,然后使用ConfigParser读取配置文件并设置默认值,然后使用解析其余的选项
parse_args()。这将使您拥有一个默认值,使用配置文件覆盖它,然后使用命令行选项覆盖它。例如:
默认,无用户输入:
$ ./argparse-partial.pyOption is "default"
配置文件中的默认值:
$ cat argparse-partial.config [Defaults]option=Hello world!$ ./argparse-partial.py -c argparse-partial.config Option is "Hello world!"
配置文件中的默认值,被命令行覆盖:
$ ./argparse-partial.py -c argparse-partial.config --option overrideOption is "override"
接下来是argprase-partial.py。
-h正确处理以获得帮助有些复杂。
import argparseimport ConfigParserimport sysdef main(argv=None): # Do argv default this way, as doing it in the functional # declaration sets it at compile time. if argv is None: argv = sys.argv # Parse any conf_file specification # We make this parser with add_help=False so that # it doesn't parse -h and print help. conf_parser = argparse.ArgumentParser( description=__doc__, # printed with -h/--help # Don't mess with format of description formatter_class=argparse.RawDescriptionHelpFormatter, # 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 = { "option":"default" } if args.conf_file: config = ConfigParser.SafeConfigParser() config.read([args.conf_file]) defaults.update(dict(config.items("Defaults"))) # Parse rest of arguments # Don't suppress add_help here so it will handle -h parser = argparse.ArgumentParser( # Inherit options from config_parser parents=[conf_parser] ) parser.set_defaults(**defaults) parser.add_argument("--option") args = parser.parse_args(remaining_argv) print "Option is "{}"".format(args.option) return(0)if __name__ == "__main__": sys.exit(main())


