Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Usage string on Click command line interface on Windows

I have a Python package called tdsmthat I have converted for the first time to a command-line interface using Armin Ronacher's Click package. I have set my scripts up within this directory structure:

enter image description here

Also I have created a setup.py file:

from setuptools import setup

setup(
    name='tdsm',
    version='0.1',
    py_modules=['tdsm.scripts.data_manager',
                'tdsm.scripts.visuals'],
    include_package_data=True,
    install_requires=[
        'click',
        'numpy',
        'scipy',
        'pandas',
        'wand',
        'matplotlib',
    ],
    entry_points='''
        [console_scripts]
        tdsm=tdsm.main:cli
    ''',
)

After a pip install --editable ., I get it working, up to a point:

# tdsm --help

Now emits:

Usage: tdsm-script.py [OPTIONS] COMMAND [ARGS]...

  TDSM standard workflow -- typical sequence of commands:

      `init <path>` or `open <path>`: to set up the project or to open
      a new session on an existing project.

      `plot`: framework for setting up plots, display layers, and styling.

Options:

  --help  Show this message and exit.

Commands:

  init     Set up a project from scratch.
  plot     Initialize a plotting session.

Note the usage statement says Usage: tdsm-script.py [OPTIONS] COMMAND [ARGS]... and not Usage: tdsm [OPTIONS] COMMAND [ARGS]... as I believe it should. Since I am expecting this to be used by non-programmers, I don't want users delving through the system looking for a non-existent file...

Poring through the docs I can see how to change every aspect of the help text but not this initial reference to tdsm-script.py. Any pointers on what I am doing wrong?

like image 463
daedalus Avatar asked Sep 13 '15 19:09

daedalus


2 Answers

The issue was resolved in the GitHub bug discussion thread by Markus Unterwaditzer. I reproduce it here for the record.

One just calls the command (or group, in my case) with an explicit prog_name argument, thus:

import click

@click.group()
def cli():
    pass

@cli.command(short_help='Set up a project from scratch.')
def init():
    pass

@cli.command(short_help='Initialise a plotting session.')
def plot():
    pass

cli(prog_name='tdsm')  # Call with explicit `prog_name`

On installing the package, the right Usage is reported:

# tdsm --help
Usage: tdsm [OPTIONS] COMMAND [ARGS]...
like image 72
daedalus Avatar answered Oct 13 '22 21:10

daedalus


You're not the only one with this problem. Judging from your screenshot it looks like you're on Windows, and it's an open bug with Click.

The problem is that Click appears to be autodetecting the program's name from argv[0]. See the code in core.py that does this.

The bug report link above suggests some hackish ways of getting around this, including monkey-patching. Unfortunately the last comment on the bug is from July 28 -- perhaps mention on that thread that you're also having this issue. Good luck.

like image 23
Christian Ternus Avatar answered Oct 13 '22 22:10

Christian Ternus