@@ -38,7 +38,7 @@ def add_short_option_name(self, short_name):
3838 def as_multi_val_param (self , nargs = u"+" ):
3939 self ._settings [u"nargs" ] = nargs
4040
41- def set_required (self , required = False ):
41+ def set_required (self , required ):
4242 self ._settings [u"required" ] = required
4343
4444
@@ -61,51 +61,51 @@ def get_auto_arg_configs(handler):
6161 """Looks at the parameter names of `handler` and builds an `ArgConfigCollection` containing
6262 `argparse` parameters based on them."""
6363 arg_configs = ArgConfigCollection ()
64+ excluded_args = [SDK_ARG_NAME , u"profile" , u"args" , u"kwargs" , u"self" ]
6465 if callable (handler ):
65- # get the number of positional and keyword args
6666 argspec = inspect .getargspec (handler )
67- num_args = len (argspec .args )
68- num_kw_args = len (argspec .defaults ) if argspec .defaults else 0
69-
70- for arg_position , key in enumerate (argspec .args ):
71- # do not create cli parameters for arguments named "sdk", "args", or "kwargs"
72- if not key in [SDK_ARG_NAME , u"args" , u"kwargs" , u"self" ]:
73- arg_config = _create_auto_args_config (
74- arg_position , key , argspec , num_args , num_kw_args
75- )
76- _set_smart_defaults (arg_config )
77- arg_configs .append (key , arg_config )
67+ filtered_argspec = {
68+ key : position for position , key in enumerate (argspec .args ) if key not in excluded_args
69+ }
70+ num_optional_args = len (argspec .defaults ) if argspec .defaults else 0
71+ num_positional_args = len (argspec .args ) - num_optional_args
72+ num_required_cli_args = len (filtered_argspec ) - num_optional_args
73+
74+ for key in filtered_argspec :
75+ arg_config = _create_auto_args_config (
76+ key , filtered_argspec [key ], num_positional_args , num_required_cli_args , argspec
77+ )
78+ _set_smart_defaults (arg_config )
79+ arg_configs .append (key , arg_config )
7880
7981 if SDK_ARG_NAME in argspec .args :
8082 _build_sdk_arg_configs (arg_configs )
8183
8284 return arg_configs
8385
8486
85- def _create_auto_args_config (arg_position , key , argspec , num_args , num_kw_args ):
87+ def _create_auto_args_config (key , position , num_positional_args , num_required_cli_args , argspec ):
8688 default = None
89+ required = None
8790 param_name = key .replace (u"_" , u"-" )
88- difference = num_args - num_kw_args
89- last_positional_arg_idx = difference - 1
91+ last_positional_arg_idx = num_positional_args - 1
92+ option_names = [ u"--{}" . format ( param_name )]
9093 # positional arguments will come first, so if the arg position
9194 # is greater than the index of the last positional arg, it's a kwarg.
92- if arg_position > last_positional_arg_idx :
95+ if position > last_positional_arg_idx :
9396 # this is a keyword arg, treat it as an optional cli arg.
94- default_value = argspec .defaults [arg_position - difference ]
95- option_names = [u"--{}" .format (param_name )]
97+ default_value = argspec .defaults [position - num_positional_args ]
9698 default = default_value
97- else :
99+ elif num_required_cli_args > 1 :
98100 # this is a positional arg, treat it as a required cli arg.
101+ required = True
102+ else :
99103 option_names = [param_name ]
100- return ArgConfig (* option_names , default = default )
104+ return ArgConfig (* option_names , default = default , required = required )
101105
102106
103107def _set_smart_defaults (arg_config ):
104108 default = arg_config .settings .get (u"default" )
105- # make a parameter allow lists as input if its default value is a list,
106- # e.g. --my-param one two three four
107- nargs = u"+" if type (default ) == list else None
108- arg_config .settings [u"nargs" ] = nargs
109109 # make the param not require a value (e.g. --enable) if the default value of
110110 # the param is a bool.
111111 if type (default ) == bool :
0 commit comments