Skip to content

Commit dbe8fd1

Browse files
Merge pull request softlayer#386 from sudorandom/issue-296
Adds common CLI options at all levels
2 parents 7c30b39 + 3e1c104 commit dbe8fd1

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

SoftLayer/CLI/core.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@
6969
VALID_FORMATS = ['raw', 'table', 'json']
7070

7171

72+
def _append_common_options(arg_doc):
73+
"""Append common options to the doc string"""
74+
default_format = 'raw'
75+
if sys.stdout.isatty():
76+
default_format = 'table'
77+
78+
arg_doc += """
79+
Standard Options:
80+
--format=ARG Output format. [Options: table, raw] [Default: %s]
81+
-C FILE --config=FILE Config file location. [Default: ~/.softlayer]
82+
--debug=LEVEL Specifies the debug noise level
83+
1=warn, 2=info, 3=debug
84+
--timings Time each API call and display after results
85+
--proxy=PROTO:PROXY_URL HTTP[s] proxy to be use to make API calls
86+
-h --help Show this screen
87+
""" % default_format
88+
return arg_doc
89+
90+
7291
class CommandParser(object):
7392
"""Helper class to parse commands.
7493
@@ -79,22 +98,17 @@ def __init__(self, env):
7998

8099
def get_main_help(self):
81100
"""Get main help text."""
82-
return __doc__.strip()
101+
return _append_common_options(__doc__).strip()
83102

84103
def get_module_help(self, module_name):
85104
"""Get help text for a module."""
86105
module = self.env.load_module(module_name)
87106
arg_doc = module.__doc__
88-
return arg_doc.strip()
107+
return _append_common_options(arg_doc).strip()
89108

90109
def get_command_help(self, module_name, command_name):
91110
"""Get help text for a specific command."""
92111
command = self.env.get_command(module_name, command_name)
93-
94-
default_format = 'raw'
95-
if sys.stdout.isatty():
96-
default_format = 'table'
97-
98112
arg_doc = command.__doc__
99113

100114
if 'confirm' in command.options:
@@ -103,18 +117,7 @@ def get_command_help(self, module_name, command_name):
103117
-y, --really Confirm all prompt actions
104118
"""
105119

106-
if '[options]' in arg_doc:
107-
arg_doc += """
108-
Standard Options:
109-
--format=ARG Output format. [Options: table, raw] [Default: %s]
110-
-C FILE --config=FILE Config file location. [Default: ~/.softlayer]
111-
--debug=LEVEL Specifies the debug noise level
112-
1=warn, 2=info, 3=debug
113-
--timings Time each API call and display after results
114-
--proxy=PROTO:PROXY_URL HTTP[s] proxy to be use to make API calls
115-
-h --help Show this screen
116-
""" % default_format
117-
return arg_doc.strip()
120+
return _append_common_options(arg_doc).strip()
118121

119122
def parse_main_args(self, args):
120123
"""Parse root arguments."""

SoftLayer/CLI/modules/summary.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
"""
2-
usage: sl summary [options]
2+
usage: sl summary [<command>] [<args>...] [options]
33
44
Display summary information about the account
5+
6+
Options:
7+
--sortby=ARG Column to sort by. options: datacenter, vlans,
8+
subnets, IPs, networking, hardware, vs
59
"""
610
# :license: MIT, see LICENSE for more details.
11+
# pylint: disable=missing-docstring
712

813
import SoftLayer
914
from SoftLayer.CLI import environment
1015
from SoftLayer.CLI import formatting
1116

1217

1318
class Summary(environment.CLIRunnable):
14-
"""
15-
usage: sl summary [options]
16-
17-
Display summary information about the account
18-
19-
Options:
20-
--sortby=ARG Column to sort by. options: datacenter, vlans,
21-
subnets, IPs, networking, hardware, vs
22-
"""
19+
__doc__ = __doc__
2320
action = None
2421

2522
def execute(self, args):

SoftLayer/managers/network.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ def summary_by_datacenter(self):
337337
unique_network = []
338338

339339
for vlan in self.list_vlans():
340-
datacenter = vlan['primaryRouter']['datacenter']
341-
name = datacenter['name']
340+
name = utils.lookup(vlan, 'primaryRouter', 'datacenter', 'name')
342341
if name not in datacenters:
343342
datacenters[name] = {
344343
'hardwareCount': 0,

SoftLayer/tests/CLI/core_tests.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ def test_primary_help(self):
197197
args = self.parser.parse_main_args(args=[])
198198
self.assertEqual({
199199
'--help': False,
200-
'-h': False,
201200
'<args>': [],
202201
'<module>': None,
203202
'<command>': None,
@@ -207,7 +206,6 @@ def test_primary_help(self):
207206
args = self.parser.parse_main_args(args=['help'])
208207
self.assertEqual({
209208
'--help': False,
210-
'-h': False,
211209
'<args>': [],
212210
'<module>': 'help',
213211
'<command>': None,
@@ -217,7 +215,6 @@ def test_primary_help(self):
217215
args = self.parser.parse_main_args(args=['help', 'module'])
218216
self.assertEqual({
219217
'--help': False,
220-
'-h': False,
221218
'<args>': ['module'],
222219
'<module>': 'help',
223220
'<command>': None,

0 commit comments

Comments
 (0)