Unandled exception if ArgparseController is missing default function
See original GitHub issueUsing ArgparseController
without a default
command results in an unhandled exception.
Code:
from cement.core.foundation import CementApp
from cement.ext.ext_argparse import ArgparseController, expose
class MyBaseController(ArgparseController):
@expose(help='<Subcommand description>')
def subcommand(self):
self.app.log.info("subcommand successful")
class MyApp(CementApp):
class Meta:
label = 'demo'
handlers = [MyBaseController]
with MyApp() as app:
app.run()
The help output looks as expected:
> python3 demo4.py --help
usage: demo [-h] [--debug] [--quiet] {subcommand} ...
optional arguments:
-h, --help show this help message and exit
--debug toggle debug output
--quiet suppress all output
sub-commands:
{subcommand}
subcommand <Subcommand description>
But running with no command raises an exception:
> python3 demo4.py
Traceback (most recent call last):
File "demo4.py", line 18, in <module>
app.run()
File "C:\Python34\lib\site-packages\cement\core\foundation.py", line 882, in run
return_val = self.controller._dispatch()
File "C:\Python34\lib\site-packages\cement\ext\ext_argparse.py", line 927, in _dispatch
(contr.__class__.__name__, func_name)) # pragma: nocover
cement.core.exc.FrameworkError: Controller function does not exist MyBaseController.default()
It seems that, if there is no default function, the best thing would be to output a parse error, as if the user had typed in an invalid function.
Otherwise, I take it that the default function is required by cement, in which case a doc update might be appropriate: http://builtoncement.com/2.10/api/ext/ext_argparse.html#cement-ext-ext-argparse
Thoughts?
(Obviously a good workaround is to create a default command and print the help text.)
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
What type of error argparse.ArgumentParser raises for missing ...
As default argparse doesn't throw an exception, but exit when an error occur. Since python 3.9, there is an exit_on_error argument, ...
Read more >argparse — Parser for command-line options, arguments and ...
If the function raises ArgumentTypeError , TypeError , or ValueError , the exception is caught and a nicely formatted error message is displayed....
Read more >Cement Framework - API Reference
Cement is an advanced CLI Application Framework for Python. Its goal is to introduce a standard, and feature-.
Read more >How to handle invalid arguments with argparse in Python?
The program given below takes two arguments – uname, pwd (user name, password). Here the criteria for setting username is, it should have...
Read more >Bug listing with status RESOLVED with resolution OBSOLETE ...
... Bug:137479 - "[java-experimental] Velocity app missing when emerging dev-java/jboss-module-varia-4.0.2" status:RESOLVED resolution:OBSOLETE severity: ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@derks I can confirm that
@expose(hide=True)
does hide the commands from thesub-commands:
list but still shows them both on the usage line:usage: conduit [-h] [--debug] [--quiet] {default,test-command} ...
and on the sub-commands shortlist:
I’d argue that for every other command other than
default
this is probably the intended behavior. But, since thedefault
command is enforced by the framework, thedefault
command showing those places is confusing clutter. Users will almost always invoke a command via<command>
as opposed to<command> default
.Having it show in those lists is both confusing from a user-perspective and aesthetically displeasing from a developer perspective.
Edit: My intended use-case for cement is to produce a CLI application with two different modes of operation, and as such, I don’t want a default implementation for the base command (other than printing usage information). The usage scenario would be:
<application> -application_options <mode_1> -mode_1_options
and<application> -application_options <mode_2> -mode_2_options
.Enforcing an implementation of the
default
command on the base controller makes this extremely difficult, and again, clutters the usage info with the irrelevantdefault
command@jtpereyda as an aside, you can use
@expose(hide=True)
so that the default (or other functions) don’t show in--help
.