@@ -13,20 +13,30 @@ def to_bytes(string):
13
13
def get_argparser ():
14
14
parser = argparse .ArgumentParser (description = "ARTIQ core device config "
15
15
"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" )
30
40
parser .add_argument ("--ddb" , default = "ddb.pyon" ,
31
41
help = "device database file" )
32
42
return parser
@@ -37,25 +47,23 @@ def main():
37
47
ddb = FlatFileDB (args .ddb )
38
48
comm = create_device (ddb .request ("comm" ), None )
39
49
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 )
42
52
if not value :
43
- print ("Key {} does not exist" .format (args .read ))
53
+ print ("Key {} does not exist" .format (args .key ))
44
54
else :
45
55
print (value )
46
- elif args .erase :
56
+ elif args .action == " erase" :
47
57
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 :
50
60
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 ())
59
67
60
68
if __name__ == "__main__" :
61
69
main ()