File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2323
2424from warnings import warn
2525
26- __version__ = "0.0.6 "
26+ __version__ = "0.1.0 "
Original file line number Diff line number Diff line change 1+ """ARGS
2+
3+ :Description: Handling of command line arguments.
4+
5+ :Author: Martin Kilbinger <martin.kilbinger@cea.fr>
6+
7+
8+ """
9+
10+
11+ def parse_options (p_def , short_options , types , help_strings ):
12+ """Parse command line options.
13+
14+ Parameters
15+ ----------
16+ p_def : dict
17+ default parameter values
18+ short_options : dict
19+ command line options short (one character) versions
20+ types : dict
21+ command line options types
22+ help_strings : dict
23+ command line options help strings
24+
25+ Returns
26+ -------
27+ options: tuple
28+ Command line options
29+ """
30+
31+ usage = "%prog [OPTIONS]"
32+ parser = OptionParser (usage = usage )
33+
34+ # Loop over default parameter values
35+ for key in p_def :
36+
37+ # Process if help string exists
38+ if key in help_strings :
39+
40+ # Set short option
41+ if key in short_options :
42+ short = short_options [key ]
43+ else :
44+ # Default is no short option
45+ short = ''
46+
47+ # Set type
48+ if key in types :
49+ typ = types [key ]
50+ else :
51+ # Default is str
52+ typ = 'string'
53+
54+ # Special case: bool type
55+ if typ == 'bool' :
56+ parser .add_option (
57+ f'{ short } ' ,
58+ f'--{ key } ' ,
59+ dest = key ,
60+ default = False ,
61+ action = 'store_true' ,
62+ help = help_strings [key ].format (p_def [key ]),
63+ )
64+ else :
65+ parser .add_option (
66+ short ,
67+ f'--{ key } ' ,
68+ dest = key ,
69+ type = typ ,
70+ default = p_def [key ],
71+ help = help_strings [key ].format (p_def [key ]),
72+ )
73+
74+ # Add verbose option
75+ parser .add_option (
76+ '-v' ,
77+ '--verbose' ,
78+ dest = 'verbose' ,
79+ action = 'store_true' ,
80+ help = f'verbose output'
81+ )
82+
83+ options , args = parser .parse_args ()
84+
85+ return options
Original file line number Diff line number Diff line change 1+ """UNIT TESTS FOR ARGS SUBPACKAGE.
2+
3+ This module contains unit tests for the args subpackage.
4+
5+ """
6+
7+ import os
8+
9+ import numpy as np
10+ from numpy import testing as npt
11+
12+ from unittest import TestCase
13+
14+ from cs_util import args
15+
16+ class ArgsTestCase (TestCase ):
17+ """Test case for the ``args`` module."""
18+
19+ def setUp (self ):
20+ """Set test parameter values."""
21+ self ._params_def = {
22+ "p_int" : 1 ,
23+ "p_float" : 0.612 ,
24+ "p_bool" : True ,
25+ "p_str" : "some_string" ,
26+ "p_str_def" : "second_string" ,
27+ "p_none" : "" ,
28+ }
29+ self ._types = {
30+ "p_int" : "int" ,
31+ "p_float" : "float" ,
32+ "p_bool" : "bool" ,
33+ "p_str" : "str" ,
34+ }
35+ self ._short_options = {
36+ "p_int" : "-i" ,
37+ "p_float" : "-f" ,
38+ "p_bool" : "-b" ,
39+ "p_str" : "-s" ,
40+ }
41+ self ._help_strings = {
42+ "p_int" : "integer option, default={}" ,
43+ "p_float" : "float option, default={}" ,
44+ "p_bool" : "bool option, default={}" ,
45+ "p_str" : "string option, default={}" .
46+ }
47+
48+ def tearDown (self ):
49+ """Unset test parameter values."""
50+ self ._params_def = None
51+ self ._types = None
52+ self ._short_options = None
53+ self ._help_strings = None
54+
55+ def test_parse_options (self ):
56+ """Test `cs_util.args.parse_options` method."""
57+ options = args .parse_options (
58+ self ._params_def ,
59+ self ._short_options ,
60+ self ._types ,
61+ self ._help_strings
62+ )
63+ print (options )
You can’t perform that action at this time.
0 commit comments