简体   繁体   English

保存python argparse文件

[英]Saving python argparse file

Argparse has a feature that called fromfile-prefix-chars , that seems to do exactly half of what I am looking for. Argparse具有一个名为fromfile-prefix-chars ,该功能似乎恰好完成了我所寻找的一半。 How can I create the file for this feature from the current command line arguments? 如何从当前命令行参数为该功能创建文件?

I have a complicated script with a parser that is automating part of a code build. 我有一个带有解析器的复杂脚本,该解析器使代码构建的一部分自动化。 The use-case is setting up the command line correctly the first time, and then basically re-running with the same arguments. 用例是第一次正确设置命令行,然后基本上使用相同的参数重新运行。

Using a file and loading from that seems like a great way to implement what I need. 使用文件并从中加载似乎是实现我所需要的一种好方法。 What seems to be missing is a simple way of writing out the initial command line to a file so the existing file parsing will work correctly. 似乎缺少的是一种将初始命令行写出到文件中的简单方法,因此现有文件解析将正常进行。

The file format is documented quite clearly: 文件格式记录得很清楚:

Arguments read from a file must by default be one per line [...] 默认情况下,从文件中读取的参数每行必须为[...]

and the code example in the documentation shows that they write a file with newlines in between: 文档中的代码示例显示他们编写的文件之间使用换行符:

 >>> with open('args.txt', 'w') as fp: ... fp.write('-f\\nbar') 

If you want to save your current command line, just write sys.argv[1:] to a file, with newlines between the arguments: 如果要保存当前命令行,只需将sys.argv[1:]写入文件,并在参数之间使用换行符:

with open('commandline_args.txt', 'w') as f:
    f.write('\n'.join(sys.argv[1:]))

Demo: 演示:

>>> from argparse import ArgumentParser
>>> import sys
>>> parser = ArgumentParser(fromfile_prefix_chars='@')
>>> parser.add_argument('--foo')
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.add_argument('bar', nargs='?')
_StoreAction(option_strings=[], dest='bar', nargs='?', const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> sys.argv[1:] = ['--foo', 'spam', 'barbaz']
>>> parser.parse_args()
Namespace(bar='barbaz', foo='spam')
>>> with open('commandline_args.txt', 'w') as f:
...     f.write('\n'.join(sys.argv[1:]))
...
17
>>> parser.parse_args(['@commandline_args.txt'])
Namespace(bar='barbaz', foo='spam')
>>> sys.argv[1:] = ['--foo=spam', 'barbaz']  # using alternate syntax
>>> parser.parse_args()
Namespace(bar='barbaz', foo='spam')
>>> with open('commandline_args.txt', 'w') as f:
...     f.write('\n'.join(sys.argv[1:]))
...
17
>>> parser.parse_args(['@commandline_args.txt'])
Namespace(bar='barbaz', foo='spam')
from argparse import ArgumentParser
import json

parser = ArgumentParser()
parser.add_argument('--seed', type=int, default=8)
parser.add_argument('--resume', type=str, default='a/b/c.ckpt')
parser.add_argument('--surgery', type=str, default='190', choices=['190', '417'])
args = parser.parse_args()

with open('commandline_args.txt', 'w') as f:
    json.dump(args.__dict__, f, indent=2)

parser = ArgumentParser()
args = parser.parse_args()
with open('commandline_args.txt', 'r') as f:
    args.__dict__ = json.load(f)

print(args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM