arguably icon indicating copy to clipboard operation
arguably copied to clipboard

how do i auto-print help when no args are provided?

Open sg-s opened this issue 1 year ago • 3 comments

hello! i love arguably! i have this simple example:

import arguably

@arguably.command
def foo__bar1():
    """some help text"""
    print("foo__bar1")


@arguably.command
def foo__bar2():
    """some help text"""
    print("foo__bar2")

def main():
    arguably.run()

assuming the name of the script is cli, when i do:

cli foo -h

i get a nice help message for the sub commands. however, when i do

cli foo

i get this error:

usage: cli foo [-h] command ...
cli foo: error: the following arguments are required: command

can i configure arguably to always print help when no args/commands are supplied? (this is similar to how cement works)

sg-s avatar Nov 20 '24 17:11 sg-s

Hey! Great question. I did this in one of my projects a while back... but I'm afraid I landed on a big hack to make it happen. It looks something like this:

try:
    result = arguably.run(always_subcommand=True, strict=True)
except ConsoleHelp:
    argv.append("-h")
    _reset_arguably(argv)
    result = arguably.run(always_subcommand=True, strict=True)

def _reset_arguably(argv: list[str]):
    """this reaches into the internals to reset everything..."""
    
# I manually raise `ConsoleHelp` in a command where I want to programmatically trigger the help message to show

oof.

This is definitely a feature that's needed.

treykeown avatar Nov 21 '24 04:11 treykeown

thanks for the response! where am i getting ConsoleHelp from? also where do i put the try/except block?

sg-s avatar Nov 21 '24 13:11 sg-s

This is something I need to add - I'll use this issue for tracking it. Shouldn't be much effort, I'll let you know!

treykeown avatar Nov 21 '24 17:11 treykeown