Created
October 10, 2015 13:06
-
-
Save tomoemon/b0cb0f167196910153f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_args(input_args, accept_args): | |
""" | |
>>> parse_args(["main.py"], {'--hoge': (1, 1)}) | |
Traceback (most recent call last): | |
Exception: error: argument --hoge: expected 1 argument(s) | |
>>> parse_args(["main.py", "a", "b", "--hoge", "w", "x", "y", "z"], {"--hoge": (2, 2)}) | |
({'--hoge': ['w', 'x']}, ['main.py', 'a', 'b', 'y', 'z']) | |
>>> parse_args(["main.py", "a", "b", "--hoge", "w", "x", "y", "z"], {"--hoge": (0, 0)}) | |
({'--hoge': []}, ['main.py', 'a', 'b', 'w', 'x', 'y', 'z']) | |
>>> parse_args(["main.py"], {'--hoge': (1, 1, ["piyo"])}) | |
({'--hoge': ['piyo']}, ['main.py']) | |
>>> parse_args(["main.py"], {"--hoge": (0, 0)}) | |
({}, ['main.py']) | |
>>> parse_args(["main.py", "--hoge"], {"--hoge": (0, 0)}) | |
({'--hoge': []}, ['main.py']) | |
>>> parse_args(["main.py", "--hoge"], {'--hoge': (0, 1)}) | |
({'--hoge': []}, ['main.py']) | |
>>> parse_args(["main.py", "--hoge", "fuga"], {'--hoge': (0, 1)}) | |
({'--hoge': ['fuga']}, ['main.py']) | |
>>> parse_args(["main.py", "--hoge"], {'--hoge': (1, 1)}) | |
Traceback (most recent call last): | |
Exception: error: argument --hoge: expected 1 argument(s) | |
>>> parse_args(["main.py", "--hoge=fuga"], {"--hoge": (0, 0)}) | |
Traceback (most recent call last): | |
Exception: error: argument --hoge: ignored explicit argument fuga | |
""" | |
input_args = input_args[:] | |
options = {} | |
right_values = [] | |
args = [] | |
while input_args: | |
arg = input_args.pop() | |
left, sep, right = arg.partition('=') | |
if left in accept_args: | |
min_length, max_length = accept_args[left][:2] | |
if right: | |
options[left] = [right] | |
else: | |
if max_length > 0: | |
options[left] = right_values[-max_length:] | |
options[left].reverse() | |
args.extend(right_values[:-max_length]) | |
else: | |
options[left] = [] | |
args.extend(right_values) | |
right_values = [] | |
if len(options[left]) < min_length: | |
raise Exception("error: argument {0}: expected {1} argument(s)".format(left, max_length)) | |
elif len(options[left]) > max_length: | |
raise Exception("error: argument {0}: ignored explicit argument {1}".format(left, options[left][0])) | |
del accept_args[left] | |
else: | |
right_values.append(left) | |
for key, value in accept_args.items(): | |
# When a key was not appeared in arguments | |
min_length = value[0] | |
if len(value) >= 3: | |
# an argument has default value | |
options[key] = value[2] | |
elif min_length > 0: | |
raise Exception("error: argument {0}: expected {1} argument(s)".format(key, min_length)) | |
args.extend(right_values) | |
args.reverse() | |
return options, args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment