14
14
15
15
class Param :
16
16
""" Provides a parameter specification to be used within a Params instance. """
17
- def __init__ (self , value , doc : Text = None , dtype : Type = None , required : bool = False , params_class = None ):
17
+ def __init__ (self , value , doc : Text = None , dtype : Type = None , required : bool = False ,
18
+ positional : bool = False , params_class = None ):
18
19
"""
19
20
Constructs a parameter specification to be used in a Params instance:
20
21
@@ -30,9 +31,12 @@ class MyParams(pp.Params):
30
31
:param doc: (Optional) document string
31
32
:param dtype: (Optional) type
32
33
:param required: default is True.
34
+ :param positional: defaults to False for making a CLI option argument like --foo. Set to True,
35
+ for a positional argument.
33
36
"""
34
37
self .params_class = params_class
35
38
self ._default_value = value
39
+ self .positional = positional
36
40
self .doc_string = doc
37
41
self .required = required
38
42
self .dtype = dtype
@@ -233,8 +237,7 @@ def from_yaml_string(cls, yaml_string: Text, check_params=False):
233
237
return cls .from_dict (lparams , return_instance = True , return_unused = False )
234
238
235
239
@classmethod
236
- @property
237
- def _open_file (cls ):
240
+ def _open_file (cls , * args , ** kwargs ):
238
241
""" Selects an open() implementation.
239
242
Tries to with Tensorflows first (tf.io.gfile.GFile)
240
243
and if not available fall backs to python's default open().
@@ -244,7 +247,7 @@ def _open_file(cls):
244
247
open_file = tf .io .gfile .GFile # pragma: no cover
245
248
except Exception : # pragma: no cover
246
249
open_file = open
247
- return open_file
250
+ return open_file ( * args , ** kwargs )
248
251
249
252
@classmethod
250
253
def from_json_file (cls , json_file , check_params = False ):
@@ -309,8 +312,13 @@ def arg_name(param_name: Text):
309
312
result = param_name .lower ().replace ("_" , "-" )
310
313
return result
311
314
315
+ def sort_positional_args (attribs : dict ):
316
+ res = list (filter (lambda t : t [1 ].positional , attribs .items ()))
317
+ res += list (filter (lambda t : not t [1 ].positional , attribs .items ()))
318
+ return res
319
+
312
320
parser = argparse .ArgumentParser ()
313
- for attr , spec in cls .__specs . items ( ):
321
+ for attr , spec in sort_positional_args ( cls .__specs ):
314
322
if spec .doc_string is None :
315
323
continue
316
324
name = arg_name (spec .name )
@@ -324,7 +332,11 @@ def arg_name(param_name: Text):
324
332
add_argument_args .update ({
325
333
"type" : _str2bool , "nargs" : "?" , "const" : True
326
334
})
327
- parser .add_argument ("--{}" .format (name ), ** add_argument_args )
335
+ if spec .positional :
336
+ del add_argument_args ['required' ]
337
+ parser .add_argument ("{}" .format (name ), ** add_argument_args )
338
+ else :
339
+ parser .add_argument ("--{}" .format (name ), ** add_argument_args )
328
340
return parser
329
341
330
342
0 commit comments