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:
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?
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]...
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.
Related
In the python script, ArgumentParser will displays the parser’s help message by default if -h or --help is supplied at the command line.
(base) D:\Study\Github\BVPA-GUI>python src\plot_auto.py -h
usage: plot_auto.py [-h] [--legend] [--figname FIGNAME]
optional arguments:
-h, --help show this help message and exit
--legend Show legend on plot.
--figname FIGNAME Specify the window title.
However it will not work if I compile the python script into exe application by pyinstaller.
D:\Study\Github\BVPA-GUI>.\bin\plotting\plot_auto.exe -h
What is the reason for this and what can I do about this?
Have you ever used sys.argv with pyinstaller?
You can check that it works well.
See argparse python official document, especially parse_args parts.
It makes a parsed arguments with the given console arguments or the given string list on function interface, parse_args(['arg1', 'arg2', ... ]).
Let's apply to your application. Very simple.
import sys, argparse
parser = argparse.ArgumentParser()
# add arguments settings ------------
.
.
.
#------------------------------------
if __name__ == "__main__":
args = parser.parse_args(sys.argv[1:])
# Your program code
I also tried same work with you.
It worked well to me.
I hope it is usefull to you too.
#Note:
In the official document said that parse_args function get default arguments from sys.argv but it doesn't seems to work with pyinstaller.
The above is just manually add such progress.
I am the author of django-persistent-settings. I have realized something odd.
The app has various management commands. When I do python setup.py --help in django-persistent-settings project, the management commands do show up:
[persistent_settings]
delvar
getvar
setvar
These commands are also tested in the library. However, these commands do not show up when I install it to a project. I wonder why that is.
I have read the related section of the docs but could not have found a warning, subsection or something alike regarding my problem. I have also checked the source code of some other projects having custom management commands such as django-simple-history or django-rest-framework. They do more or less the same thing.
Is there maybe something I don't know? An issue I haven't seen but encountered somehow?
Reanimating the Unexpected Behavior
Make sure Django 2 is installed and create a dummy project.
django-admin --version
# django 2.2.12 or something similar
django-admin createproject foo
cd foo
virtualenv .venv
source .venv/bin/activate
pip install "django<3" django-persistent-settings
Open up foo/settings.py and add it INSTALLED_APPS:
INSTALLED_APPS = [
# ...
"persistent_settings"
]
List commands:
python setup.py --help
And the commands do not show up.
Environment
Python 3.8
Django 2.2.12
I forgot to add management package to setup.py. A dumb mistake.
Remember to add your management command subpackages to packages kwarg in setup.py.
setup(
# ...
packages=(
"persistent_settings",
"persistent_settings.migrations",
"persistent_settings.templatetags",
"persistent_settings.management", # add this line
"persistent_settings.management.commands", # and this line
)
# ...
)
I made a simple CLI using Typer and Pillow to change image opacity and this program only have one option: opacity.
But when I run python opacity.py --help it gives me the two typerCLI options:
Options:
--install-completion [bash|zsh|fish|powershell|pwsh]
Install completion for the specified
shell.
--show-completion [bash|zsh|fish|powershell|pwsh]
Show completion for the specified
shell, to copy it or customize the
installation.
--help Show this message and exit.
There's a way to disable it? I didn't find on docs.
I met the same problem today, i couldn't find anything except this question so dived in the source to find how Typer automatically adds this line in app, so i found this, when Typer initialiazing itself it automatically sets add_completion to True
class Typer:
def __init__(add_completion: bool = True)
So when you initialiazing your app you can add this
app = typer.Typer(add_completion=False)
This is how it looks after you add add_completion=False
Usage: main.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
As of version 0.7.0, Typer now excludes these by default if you use the typer.run() API.
Example:
# foo.py
import typer
def main():
pass
if __name__ == "__main__":
typer.run(main)
Produces:
$ pip install typer[all]==0.7.0
$ python foo.py --help
Usage: foo.py [OPTIONS]
╭─ Options ───────────────────────────────────────╮
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────╯
I am trying to learn chef and very new at it.
I have a requirements.txt file which I'm trying to execute through a chef recipe to install some Python modules. I have tried different variations of the code, however I feel like I'm missing something. I have tried writing the following code in the chef recipe:
template '/etc' do
source 'requirements.txt.erb'
owner 'root'
group 'root'
mode '0644'
end
execute 'requirements.txt' do
command 'pip install -r requirements.txt'
action 'run'
end
I was expecting that the requirements file will be called when I run vagrant up and the modules/dependencies of the app will be installed. However, I get this error:
Error executing action run on resource 'execute[requirements.txt]'
Check this example from https://docs.chef.io/resource_execute.html
execute 'upgrade script' do
command 'php upgrade-application.php && touch /var/application/.upgraded'
creates '/var/application/.upgraded'
action :run
end
You need to specify the run action as shown above (:run) rather than with single quotes as shown in your posted example.
if i am not mistaken, i think that coderanger developed a cookbook named python-poise, which has pip_requirements chef resource. it looks like:
pip_requirements '/opt/myapp/requirements.txt'
I know this is possible, I'm a newbie on Python. Looked at nose. But nose is not what we require. There should be some way to customize when I run python unittest.py --help on my own. Can any python people throw some light on this on how or where we can change. I'm using python 2.6.6
Alright, you're looking at customizing unittest.TestProgram. You'll want to create something like myunittest.py and in that module you'll do something like:
# myunittest.py
from unittest import TestProgram
class MyTestProgram(TestProgram):
USAGE = """\
Usage: %(progName)s [options] [test] [...]
Options:
-h, --help Show this message
-v, --verbose Verbose output
-q, --quiet Minimal output
Examples:
%(progName)s - run default set of tests
%(progName)s MyTestSuite - run suite 'MyTestSuite'
%(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
%(progName)s MyTestCase - run all 'test*' test methods
in MyTestCase
"""
# additional changes here...
main = MyTestProgram
if __name__ == "__main__":
main(module=None)
Just changing USAGE may be all you really care about as that's the text printed by python unittest.py --help but you could obviously customize more.
Note this is specific to Python2.6. Python 2.7+ uses unittest2 which I'm not really familiar with at this point.