From 446e7ca2ed4ad7fe11519ad1744a2da7ca1bc4de Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Sat, 18 Jul 2015 17:45:55 +0100 Subject: Add subcommand usage getter --- cliapp/app.py | 4 ++++ example2.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cliapp/app.py b/cliapp/app.py index 9812f0d..1aa35cb 100644 --- a/cliapp/app.py +++ b/cliapp/app.py @@ -287,6 +287,10 @@ class Application(object): return cliapp.TextFormat(*a, **kw) + def get_subcommand_usage(self, cmd): # pragma: no cover + return 'usage: %s %s %s' % (self.settings.progname, + cmd, self.cmd_synopsis[cmd]) + def _help_helper(self, args, show_all): # pragma: no cover try: width = int(os.environ.get('COLUMNS', '78')) diff --git a/example2.py b/example2.py index 972e880..fcee592 100644 --- a/example2.py +++ b/example2.py @@ -28,8 +28,8 @@ import cliapp class ExampleApp(cliapp.Application): cmd_synopsis = { - 'greet': '[USER]...', - 'insult': '[USER]...', + 'greet': 'USER...', + 'insult': 'USER...', } def cmd_greet(self, args): @@ -39,6 +39,9 @@ class ExampleApp(cliapp.Application): but terse form of greeting. ''' + if len(args) == 0: + raise cliapp.AppException(self.get_subcommand_usage('greet')) + for arg in args: self.output.write('greetings, %s\n' % arg) @@ -49,6 +52,9 @@ class ExampleApp(cliapp.Application): a prat, and needs to be told off. This is the command for that. ''' + if len(args) == 0: + raise cliapp.AppException(self.get_subcommand_usage('insult')) + for arg in args: self.output.write('you suck, %s\n' % arg) -- cgit v1.2.1