-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathargparse_module.py
98 lines (70 loc) · 3.35 KB
/
argparse_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
#coding:utf8
'''
NOTICE:
If parser.parse_args() is used in a module, please avoid using it in another module again !!!
'''
import argparse
# # Outlines: special case
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''
DESCRIPTION with linebreak
''')
from datetime import datetime
get_date = lambda x: datetime.strptime(x, "%Y%m%d").date()
parser.add_argument('--train_s', type=get_date, help='datetime args', required=True)
parser.add_argument('--multi_arg', nargs='*')
args = parser.parse_args('--train_s 20121010'.split())
getattr(args, "test_attr", "test") # some time we want to be compatible
print(args)
# NOTE: 默认操作是 override,只要action 没有变化,就都是override
print(parser.parse_args('--train_s 20121010 --train_s 20220202 --multi_arg good haha xixi --multi_arg override'.split()))
# # Outlines: boolean
parser2 = argparse.ArgumentParser()
parser2.add_argument('--exist-then-true', action='store_true', help='the exist_then_true will be True if present, otherwise False')
print(parser2.parse_args())
print(parser2.parse_args("--exist-then-true".split()))
# 变量名中的`-` 会被换成 `_`
# %% [markdown]
# # Outlines: 多重变量
parser3 = argparse.ArgumentParser()
# multi args
parser3.add_argument('--file', action='append')
parser3.add_argument('--file_with_default', action='append', default=[1,2,3])
print(parser3.parse_args("".split())) # 没有默认值且没有传参时, 默认为None
print(parser3.parse_args("--file f1".split())) # 传了参数就变成 list
print(parser3.parse_args("--file_with_default fwd".split())) # 有默认值时,直接往默认值上传参
# # Outlines: 我训练模型常常会用的多次 kwargs update (方便设置默认dict)
class DictUpdate(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, {})
if items is None: items = {}
if isinstance(values, str):
values = eval(values)
items.update(values)
setattr(namespace, self.dest, items)
parser4 = argparse.ArgumentParser()
parser4.add_argument('--kwargs', action=DictUpdate)
parser4.add_argument('--kwargs_wd', action=DictUpdate, default={})
print(parser4.parse_args("".split()))
print(parser4.parse_args(["--kwargs", "{1: 2}", "--kwargs", "{3: 4}"]))
# # Outlines: 多重变量
parser5 = argparse.ArgumentParser()
parser5.add_argument('--multi_arg', nargs='*')
parser5.add_argument('--multi_arg_with_default', nargs='*', default=[1,2,3])
parser5.add_argument('default_args', nargs='*')
print('''For normal multi args, it will be none of no arguments provided.
For default args, it will return a list even if no arguments provided.''')
print(parser5.parse_args(''.split()))
print(parser5.parse_args('--multi_arg default_args'.split()))
print("The arguments will comsumed by normal arguments.")
print(parser5.parse_args('--multi_arg 1 2 3 4'.split()))
print(parser5.parse_args('1 2 3'.split()))
# TODO: sub commands
# https://docs.python.org/2.7/library/argparse.html#sub-commands
# 对于两者只有一个是required的参数(比如config和其他的参数),
# sub commands是非常好的选择.
# ArgumentParser.add_mutually_exclusive_group只能实现多个单个argument互斥
# Turn object into CLI
# https://github.com/google/python-fire