class CmdCommand(object):
- def __init__(self, sig, desc, module=None, perm=None, flags=0, poll=None):
- self.sig = [s for s in sig if isinstance(s, str)]
- self.params = sorted([CmdParam(**s) for s in sig if not isinstance(s, str)],
+ def __init__(self, prefix, args, desc, module=None, perm=None, flags=0, poll=None):
+ self.prefix = prefix
+ self.params = sorted([CmdParam(**arg) for arg in args],
key=lambda p: p.req, reverse=True)
self.help = desc
self.module = module
self.flags = Flags(flags)
self.needs_overload = False
- def prefix(self):
- return ' '.join(self.sig)
-
def is_reasonably_simple(self):
if len(self.params) > 3:
return False
def mk_bash_example(self):
simple = self.is_reasonably_simple()
- line = ' '.join(['ceph', self.prefix()] + [p.mk_bash_example(simple) for p in self.params])
+ line = ' '.join(['ceph', self.prefix] + [p.mk_bash_example(simple) for p in self.params])
return line
class Sig:
@staticmethod
- def _param_to_sig(p):
+ def _parse_arg_desc(desc):
try:
- return {kv.split('=')[0]: kv.split('=')[1] for kv in p.split(',') if kv}
+ return {kv.split('=')[0]: kv.split('=')[1] for kv in desc.split(',') if kv}
except IndexError:
- return p
+ return desc
+
+ @staticmethod
+ def parse_cmd(cmd):
+ parsed = [Sig._parse_arg_desc(s) or s for s in cmd.split()]
+ prefix = [s for s in parsed if isinstance(s, str)]
+ params = [s for s in parsed if not isinstance(s, str)]
+ return ' '.join(prefix), params
@staticmethod
- def from_cmd(cmd):
- sig = cmd.split()
- return [Sig._param_to_sig(s) or s for s in sig]
+ def parse_args(args):
+ return [Sig._parse_arg_desc(arg) for arg in args.split()]
TEMPLATE = '''
{% for command in commands %}
-{{ command.prefix() }}
-{{ command.prefix() | length * '^' }}
+{{ command.prefix }}
+{{ command.prefix | length * '^' }}
{{ command.help | wordwrap(70)}}
def _normalize_command(self, command):
if 'handler' in command:
del command['handler']
- command['sig'] = Sig.from_cmd(command['cmd'])
- del command['cmd']
+ if 'cmd' in command:
+ command['prefix'], command['args'] = Sig.parse_cmd(command['cmd'])
+ del command['cmd']
+ else:
+ command['args'] = Sig.parse_args(command['args'])
command['flags'] = (1 << 3)
command['module'] = 'mgr'
return command
commands = sum([self._collect_module_commands(name) for name in modules], [])
cmds = [CmdCommand(**self._normalize_command(c)) for c in commands]
cmds = [cmd for cmd in cmds if 'hidden' not in cmd.flags]
- cmds = sorted(cmds, key=lambda cmd: cmd.sig)
+ cmds = sorted(cmds, key=lambda cmd: cmd.prefix)
self._render_cmds(cmds)
return []
def _normalize_command(self, command):
if 'handler' in command:
del command['handler']
- command['sig'] = Sig.from_cmd(command['cmd'])
+ command['prefix'], command['args'] = Sig.parse_cmd(command['cmd'])
del command['cmd']
return command
commands = self._parse_headers(headers)
cmds = [CmdCommand(**self._normalize_command(c)) for c in commands]
cmds = [cmd for cmd in cmds if 'hidden' not in cmd.flags]
- cmds = sorted(cmds, key=lambda cmd: cmd.sig)
+ cmds = sorted(cmds, key=lambda cmd: cmd.prefix)
self._render_cmds(cmds)
return []