Coder Social home page Coder Social logo

Comments (5)

mumumumu avatar mumumumu commented on May 7, 2024

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.

contactr2m avatar contactr2m commented on May 7, 2024

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.

vimalloc avatar vimalloc commented on May 7, 2024

+1

from click.

vimalloc avatar vimalloc commented on May 7, 2024

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.

dawranliou avatar dawranliou commented on May 7, 2024

Not sure when this is added but you can now use User Input Prompts

from click.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.