Skip to content

Commit afc7092

Browse files
committedJun 17, 2015
artiq_coreconfig: use subparsers for arg parsing
1 parent 2f8a67c commit afc7092

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed
 

Diff for: ‎artiq/frontend/artiq_coreconfig.py

+36-28
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,30 @@ def to_bytes(string):
1313
def get_argparser():
1414
parser = argparse.ArgumentParser(description="ARTIQ core device config "
1515
"remote access")
16-
parser.add_argument("-r", "--read", type=to_bytes,
17-
help="read key from core device config")
18-
parser.add_argument("-w", "--write", nargs=2, action="append", default=[],
19-
metavar=("KEY", "STRING"), type=to_bytes,
20-
help="write key-value records to core device config")
21-
parser.add_argument("-f", "--write-file", nargs=2, action="append",
22-
type=to_bytes, default=[], metavar=("KEY", "FILENAME"),
23-
help="write the content of a file into core device "
24-
"config")
25-
parser.add_argument("-e", "--erase", action="store_true",
26-
help="erase core device config")
27-
parser.add_argument("-d", "--delete", action="append", default=[],
28-
type=to_bytes,
29-
help="delete key from core device config")
16+
subparsers = parser.add_subparsers(dest="action")
17+
subparsers.required = True
18+
p_read = subparsers.add_parser("read",
19+
help="read key from core device config")
20+
p_read.add_argument("-k", "--key", type=to_bytes, required=True,
Has conversations. Original line has conversations.
21+
help="key to be read from core device config")
22+
p_write = subparsers.add_parser("write",
23+
help="write key-value records to core "
24+
"device config")
25+
p_write.add_argument("-s", "--string", nargs=2, action="append",
26+
default=[], metavar=("KEY", "STRING"), type=to_bytes,
27+
help="key-value records to be written to core device "
28+
"config")
29+
p_write.add_argument("-f", "--file", nargs=2, action="append",
30+
type=to_bytes, default=[],
31+
metavar=("KEY", "FILENAME"),
32+
help="key and file whose content to be written to "
33+
"core device config")
34+
subparsers.add_parser("erase", help="erase core device config")
35+
p_delete = subparsers.add_parser("delete",
36+
help="delete key from core device config")
37+
p_delete.add_argument("-k", "--key", action="append", default=[],
Has conversations. Original line has conversations.
38+
type=to_bytes, required=True,
39+
help="key to be deleted from core device config")
3040
parser.add_argument("--ddb", default="ddb.pyon",
3141
help="device database file")
3242
return parser
@@ -37,25 +47,23 @@ def main():
3747
ddb = FlatFileDB(args.ddb)
3848
comm = create_device(ddb.request("comm"), None)
3949

40-
if args.read:
41-
value = comm.flash_storage_read(args.read)
50+
if args.action == "read":
51+
value = comm.flash_storage_read(args.key)
4252
if not value:
43-
print("Key {} does not exist".format(args.read))
53+
print("Key {} does not exist".format(args.key))
4454
else:
4555
print(value)
46-
elif args.erase:
56+
elif args.action == "erase":
4757
comm.flash_storage_erase()
48-
elif args.delete:
49-
for key in args.delete:
58+
elif args.action == "delete":
59+
for key in args.key:
5060
comm.flash_storage_remove(key)
51-
else:
52-
if args.write:
53-
for key, value in args.write:
54-
comm.flash_storage_write(key, value)
55-
if args.write_file:
56-
for key, filename in args.write_file:
57-
with open(filename, "rb") as fi:
58-
comm.flash_storage_write(key, fi.read())
61+
elif args.action == "write":
62+
for key, value in args.string:
63+
comm.flash_storage_write(key, value)
64+
for key, filename in args.file:
65+
with open(filename, "rb") as fi:
66+
comm.flash_storage_write(key, fi.read())
5967

6068
if __name__ == "__main__":
6169
main()

0 commit comments

Comments
 (0)
Please sign in to comment.