@@ -13,6 +13,19 @@ def emit(self, record):
1313
1414DEFAULT_CONFIG_FILES = ['lightbeam.yaml' , 'lightbeam.yml' ]
1515
16+ # use a dictionary here so that command strings can be accessed with a lookup.
17+ # This helps enforce usage of this structure
18+ ALLOWED_COMMANDS = {
19+ "validate" : "validate" ,
20+ "send" : "send" ,
21+ "validate+send" : "validate+send" ,
22+ "delete" : "delete" ,
23+ "truncate" : "truncate" ,
24+ "count" : "count" ,
25+ "fetch" : "fetch" ,
26+ }
27+ command_list = ', ' .join (f"'{ c } '" for c in ALLOWED_COMMANDS .values ())
28+
1629# Set up logging
1730handler = ExitOnExceptionHandler ()
1831formatter = logging .Formatter ("%(asctime)s.%(msecs)03d %(name)s %(levelname)s %(message)s" , "%Y-%m-%d %H:%M:%S" )
@@ -35,7 +48,7 @@ def main(argv=None):
3548 parser .add_argument ('command' ,
3649 nargs = "?" ,
3750 type = str ,
38- help = 'the command to run: `validate`, `send`, `validate+send`, or `delete` '
51+ help = f 'the command to run: { command_list } '
3952 )
4053 parser .add_argument ("-c" , "--config-file" ,
4154 nargs = "?" ,
@@ -98,7 +111,18 @@ def main(argv=None):
98111
99112 defaults = { "selector" :"*" , "params" : "" , "older_than" : "" , "newer_than" : "" , "resend_status_codes" : "" , "results_file" : "" }
100113 parser .set_defaults (** defaults )
101- args , remaining_argv = parser .parse_known_args ()
114+ args , unknown_args = parser .parse_known_args ()
115+ if len (unknown_args ) > 0 :
116+ unknown_args_str = ', ' .join (f"`{ c } `" for c in unknown_args )
117+ print (f"unknown arguments { unknown_args_str } passed, use -h flag for help" )
118+ exit (1 )
119+
120+ if args .command not in ALLOWED_COMMANDS .values ():
121+ if args .command is None :
122+ logger .error (f"no command provided. Use one of ({ command_list } ), see -h flag for help" )
123+ else :
124+ logger .error (f"unknown command '{ args .command } ' passed, use -h flag for help" )
125+ exit (1 )
102126
103127 if args .version :
104128 lb_dir = os .path .dirname (os .path .abspath (__file__ ))
@@ -108,9 +132,6 @@ def main(argv=None):
108132 print (f"lightbeam, version { VERSION } " )
109133 exit (0 )
110134
111- if args .command not in ['validate' , 'send' , 'validate+send' , 'delete' , 'truncate' , 'count' , 'fetch' ]:
112- logger .error ("Please specify a command to run: `count`, `fetch`, `validate`, `send`, `validate+send`, `delete`, or `truncate`. (Try the -h flag for help.)" )
113-
114135 if not args .config_file :
115136 for file in DEFAULT_CONFIG_FILES :
116137 test_file = os .path .join ("." , file )
@@ -140,18 +161,18 @@ def main(argv=None):
140161 )
141162 try :
142163 logger .info ("starting..." )
143- if args .command == 'count' : lb .counter .count ()
144- elif args .command == 'fetch' : lb .fetcher .fetch ()
145- elif args .command == 'validate' : lb .validator .validate ()
146- elif args .command == 'send' : lb .sender .send ()
147- elif args .command == 'validate+send' :
164+ if args .command == ALLOWED_COMMANDS [ 'count' ] : lb .counter .count ()
165+ elif args .command == ALLOWED_COMMANDS [ 'fetch' ] : lb .fetcher .fetch ()
166+ elif args .command == ALLOWED_COMMANDS [ 'validate' ] : lb .validator .validate ()
167+ elif args .command == ALLOWED_COMMANDS [ 'send' ] : lb .sender .send ()
168+ elif args .command == ALLOWED_COMMANDS [ 'validate+send' ] :
148169 lb .validator .validate ()
149170 lb .sender .send ()
150- elif args .command == 'delete' : lb .deleter .delete ()
151- elif args .command == 'truncate' : lb .truncator .truncate ()
171+ elif args .command == ALLOWED_COMMANDS [ 'delete' ] : lb .deleter .delete ()
172+ elif args .command == ALLOWED_COMMANDS [ 'truncate' ] : lb .truncator .truncate ()
152173 lb .logger .info ("done!" )
153174 except Exception as e :
154175 logger .exception (e , exc_info = lb .config ["show_stacktrace" ])
155176
156177if __name__ == "__main__" :
157- sys .exit (main ())
178+ sys .exit (main ())
0 commit comments