Skip to content

Commit 5ff7360

Browse files
committed
[bin] Positional config param is now --config or -c
Explicit is better than implicit
1 parent 6f8380d commit 5ff7360

File tree

3 files changed

+58
-47
lines changed

3 files changed

+58
-47
lines changed

bin/netjsonconfig

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,50 @@ You should have received a copy of the GNU General Public License
2020
along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
"""
2222

23-
parser = argparse.ArgumentParser(description='Converts a NetJSON DeviceConfiguration object'
24-
'to working router configurations.',
23+
parser = argparse.ArgumentParser(description='Converts a NetJSON DeviceConfiguration object '
24+
'to native router configurations.',
2525
epilog=license,
2626
prog='netjsonconfig')
2727

28-
parser.add_argument('config',
28+
config = parser.add_argument_group('input')
29+
30+
config.add_argument('--config', '-c',
2931
action='store',
3032
type=str,
3133
help='config file or string, must be valid NetJSON DeviceConfiguration')
3234

33-
parser.add_argument('--templates', '-t',
35+
config.add_argument('--templates', '-t',
3436
nargs='*', # zero or more
3537
action='store',
3638
type=str,
3739
default=[],
3840
help='list of template config files or strings separated by space')
3941

40-
parser.add_argument('--backend', '-b',
41-
choices=['openwrt', 'openwisp'],
42-
action='store',
43-
type=str,
44-
help='Configuration backend: openwrt or openwisp')
42+
output = parser.add_argument_group('output')
4543

46-
parser.add_argument('--method', '-m',
47-
choices=['generate', 'render'],
48-
action='store',
49-
help='Backend method to use. '\
50-
'"generate" returns a tar.gz archive as output; '
51-
'"render" returns the configuration in text format')
52-
53-
parser.add_argument('--verbose',
54-
action='store_true',
55-
default=False,
56-
help='verbose output')
57-
58-
parser.add_argument('--version', '-v',
59-
action='version',
60-
version=netjsonconfig.get_version())
44+
output.add_argument('--backend', '-b',
45+
choices=['openwrt', 'openwisp'],
46+
action='store',
47+
type=str,
48+
help='Configuration backend: openwrt or openwisp')
49+
50+
output.add_argument('--method', '-m',
51+
choices=['render', 'generate'],
52+
action='store',
53+
help='Backend method to use. '\
54+
'"render" returns the configuration in text format'\
55+
'"generate" returns a tar.gz archive as output; ')
56+
57+
debug = parser.add_argument_group('debug')
58+
59+
debug.add_argument('--verbose',
60+
action='store_true',
61+
default=False,
62+
help='verbose output')
63+
64+
debug.add_argument('--version', '-v',
65+
action='version',
66+
version=netjsonconfig.get_version())
6167

6268
def _load(config):
6369
"""

docs/source/general/commandline_utility.rst

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,50 @@ languages (via system calls).
99
Check out the available options yourself with::
1010

1111
$ netjsonconfig --help
12-
usage: netjsonconfig [-h] [--templates [TEMPLATES [TEMPLATES ...]]]
12+
usage: netjsonconfig [-h] [--config CONFIG]
13+
[--templates [TEMPLATES [TEMPLATES ...]]]
1314
[--backend {openwrt,openwisp}]
14-
[--method {generate,render}] [--verbose] [--version]
15-
config
15+
[--method {render,generate}] [--verbose] [--version]
1616

17-
Converts a NetJSON DeviceConfiguration objectto working router configurations.
18-
19-
positional arguments:
20-
config config file or string, must be valid NetJSON
21-
DeviceConfiguration
17+
Converts a NetJSON DeviceConfiguration object to native router configurations.
2218

2319
optional arguments:
2420
-h, --help show this help message and exit
21+
22+
input:
23+
--config CONFIG, -c CONFIG
24+
config file or string, must be valid NetJSON
25+
DeviceConfiguration
2526
--templates [TEMPLATES [TEMPLATES ...]], -t [TEMPLATES [TEMPLATES ...]]
2627
list of template config files or strings separated by
2728
space
29+
30+
output:
2831
--backend {openwrt,openwisp}, -b {openwrt,openwisp}
2932
Configuration backend: openwrt or openwisp
30-
--method {generate,render}, -m {generate,render}
31-
Backend method to use. "generate" returns a tar.gz
32-
archive as output; "render" returns the configuration
33-
in text format
33+
--method {render,generate}, -m {render,generate}
34+
Backend method to use. "render" returns the
35+
configuration in text format"generate" returns a
36+
tar.gz archive as output;
37+
38+
debug:
3439
--verbose verbose output
3540
--version, -v show program's version number and exit
3641

3742
Here's the common use cases explained::
3843

3944
# generate tar.gz from a NetJSON DeviceConfiguration object and save it to a file
40-
netjsonconfig --backend openwrt --method generate config.json > config.tar.gz
45+
netjsonconfig --config config.json --backend openwrt --method generate > config.tar.gz
4146

4247
# see output of OpenWrt render method
43-
netjsonconfig --backend openwrt --method render config.json
48+
netjsonconfig --config config.json --backend openwrt --method render
4449

4550
# abbreviated options
46-
netjsonconfig -b openwrt -m render config.json
51+
netjsonconfig -c config.json -b openwrt -m render
4752

4853
# passing a JSON string instead of a file path
49-
netjsonconfig -b openwrt -m render '{"general": { "hostname": "example" }}'
54+
netjsonconfig -c '{"general": { "hostname": "example" }}' -b openwrt -m render
5055

5156
Using templates::
5257

53-
netjsonconfig config.json -t template1.json template2.json -b openwrt -m render
58+
netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m render

tests/test_bin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ class TestBin(unittest.TestCase, _TabsMixin):
1111
"""
1212
def test_file_not_found(self):
1313
with self.assertRaises(subprocess.CalledProcessError):
14-
output = subprocess.check_output("netjsonconfig WRONG -b openwrt -m generate", shell=True)
14+
output = subprocess.check_output("netjsonconfig -c WRONG -b openwrt -m generate", shell=True)
1515

1616
def test_invalid_netjson(self):
17-
command = '''netjsonconfig '{ "interfaces":["w"] }' -b openwrt -m render'''
17+
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m render'''
1818
with self.assertRaises(subprocess.CalledProcessError):
1919
output = subprocess.check_output(command, shell=True)
2020

2121
def test_invalid_netjson_verbose(self):
22-
command = '''netjsonconfig '{ "interfaces":["w"] }' -b openwrt -m render --verbose'''
22+
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m render --verbose'''
2323
with self.assertRaises(subprocess.CalledProcessError):
2424
output = subprocess.check_output(command, shell=True)
2525

2626
def test_empty_netjson(self):
27-
output = subprocess.check_output("netjsonconfig '{}' -b openwrt -m render", shell=True)
27+
output = subprocess.check_output("netjsonconfig -c '{}' -b openwrt -m render", shell=True)
2828
self.assertEqual(output.decode(), '')
2929

3030
def test_templates(self):
@@ -59,15 +59,15 @@ def test_templates(self):
5959
}
6060
]
6161
})
62-
command = """netjsonconfig '{0}' -b openwrt -m render --templates '{1}' '{2}'"""
62+
command = """netjsonconfig --config '{0}' -b openwrt -m render --templates '{1}' '{2}'"""
6363
command = command.format(config, template1, template2)
6464
output = subprocess.check_output(command, shell=True).decode()
6565
self.assertIn("hostname 'template_test'", output)
6666
self.assertIn("interface 'eth0'", output)
6767
self.assertIn("interface 'wlan0'", output)
6868

6969
def test_invalid_template(self):
70-
command = "netjsonconfig '{}' -b openwrt -t WRONG -m render"
70+
command = "netjsonconfig -c '{}' -b openwrt -t WRONG -m render"
7171
try:
7272
output = subprocess.check_output(command, shell=True)
7373
except subprocess.CalledProcessError as e:

0 commit comments

Comments
 (0)