Comments (5)
I was looking into similar functionality today, from the wiki it looks like you need to catch the SystemExit exception. (http://click.pocoo.org/api/?highlight=systemexit#click.BaseCommand.main)
I think you might want to try something like:
while True:
try:
# your command
except SystemExit as e:
if e.code:
break
The reason for the e.code conditional is so you can still terminate the program with Ctrl-C
from click.
Is there any example of achieving this ? I currently use cmd/cmd2 for interactive shell but it does not handle arguments with spaces etc.. making commands arguments available through click would be better option.
has anyone implemented it before ? or any guidance would be appreciated.
Thank you
from click.
+1
from click.
I've been putting some work into this. I'm sure it will need more work (I'll try and update this if I make changes), but here is an example of how it might be done (with handling options).
import click
import cmd
import sys
from click import BaseCommand, UsageError
class REPL(cmd.Cmd):
def __init__(self, ctx):
cmd.Cmd.__init__(self)
self.ctx = ctx
def default(self, line):
subcommand = line.split()[0]
args = line.split()[1:]
subcommand = cli.commands.get(subcommand)
if subcommand:
try:
subcommand.parse_args(self.ctx, args)
self.ctx.forward(subcommand)
except UsageError as e:
print(e.format_message())
else:
return cmd.Cmd.default(self, line)
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
if ctx.invoked_subcommand is None:
repl = REPL(ctx)
repl.cmdloop()
@cli.command()
@click.option('--foo', required=True)
def a(foo):
print("a")
print(foo)
@cli.command()
@click.option('--foo', required=True)
def b(foo):
print("b")
print(foo)
if __name__ == "__main__":
cli()
from click.
Not sure when this is added but you can now use User Input Prompts
from click.
Related Issues (20)
- Support for negative numbers
- DOC: Add chapter on how to debug a CLI with pdb HOT 2
- Allow Hiding of Range Values for IntRange Option in --help HOT 2
- Revisiting stderr flush in CLIRunner
- feature: allow substituting command_path in command help and epilog text
- Wront Usage when running a module with python -m HOT 1
- format_help_text does not format text in a group HOT 5
- Conditionally apply options to input
- Click performance is poor HOT 1
- Request to document some basic features explicitly HOT 2
- show envvar in error hint HOT 3
- Broken message about invalid argument value for template "File ... is a directory"
- Missing return value in context manager example (Managing resources)?
- zsh completion still misbehaving in the presence of `:`
- Setting color=True in runner.invoke on windows strips ansi codes
- Better i18n with gettext: use class-based API
- Broken code example in docs under Advanced Patterns
- Utility to introspect known command arguments HOT 3
- Autocorrect wrong/misspelled option with the suggested unique possibility. HOT 1
- Docs wrongly links PRs and Issues to flask
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from click.