Questions tagged [python-click]
Click is a Python library for creating command line interfaces.
430
questions
1
vote
1
answer
25
views
Why doesn't python-click pass context between subcommands?
I'm trying to pass contexts between two subcommands with python-click. Here's an MWE:
import click
@click.group(chain=True)
def cli() -> None:
pass
@cli.command()
@click.pass_context
def fn1(...
2
votes
1
answer
38
views
Python Click: handling cli usage Exceptions in a chained MultiCommand with context resource
Background
Following examples in the Click documentation (specifically custom multi commands, multi command pipelines and managing resources) I've written a CLI application similar to the Image ...
0
votes
1
answer
20
views
What is the best way to wrap multiple existing CLIs in one CLI using python
I want to wrap multiple CLIs and make one parent CLI that will basically call the other CLIs under the hood.
To give an example, let's say I want to make my_cli that will wrap the git and cmake clis. ...
0
votes
1
answer
32
views
Python click module. Show color in windows and mac
I need to show messages with color in MacOS and in Windows terminal. I used click, and tried that code:
def print_error(
message, # type: str
): # type: (...) -> None
error_word = ...
0
votes
0
answers
50
views
Change value dynamically with click.option callback function
So I want to Change default list in program by reading option from command line.
Using this answer by @Stephen Rauch and add simple option to click with simple callback function set_list() it seems to ...
0
votes
0
answers
52
views
Python click: AttributeError: 'Command' object has no attribute '__name__'
I am creating a simple CLI app and receiving the following error, I have no idea what is happening please help. The code works fine if I remove the click library (and the associated code), so there ...
0
votes
1
answer
128
views
join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
I am making a CLI tool using python and click. I have a command called ext that has two options --type and --path and then another command ext. The function organizes the files in the specified path ...
0
votes
1
answer
34
views
Splitting python click options betwen functions
My codebase has multiple scripts, say sc1.py and sc2.py. Their code looks like this (just replace 1 for 2 to imagine what the other is like):
@click.command()
@click.option(--op1, default="def1&...
0
votes
1
answer
48
views
Elegant way of integrating for loop with a flag in Python
I am using click to create a command line tool that performs some data preprocessing. Until now, I have basically survived using click.option() as flag with some if statements in my code so that I can ...
0
votes
0
answers
30
views
Creating sub commands groups in Click CLI
Is it possible to group commands in sections with Click library in Python?
Flutter cli for instance, you have the section, Flutter SDK, Project, Tools & Devices.
0
votes
1
answer
37
views
How to pass only one option in Python Click
I am writing a command line interface using python click library and I want to enforce users to pass only one option at a time to the click command and it should return an error if user pass both the ...
0
votes
1
answer
59
views
Combine cloup.group and click-default-group
I'm using cloup for my CLI for its constraints feature.
I have some commands a and b which have no common arguments.
import cloup
@cloup.group()
def cli():
pass
@cli.command(show_constraints=...
0
votes
1
answer
53
views
Python: multiprocessing with click
I have the following files:
binreader/
├─ packet/
│ ├─ __init__.py
│ ├─ aggregator.py
│ ├─ parser.py
│ ├─ uploader.py
├─ __init__.py
├─ __main__.py
├─ upload_concurrent.py
Code that reproduces ...
0
votes
1
answer
48
views
how to specify a required option out of 3 in python `click`
I am trying to use https://click.palletsprojects.com/en/8.0.x/options/
I have a use case where one out of these 3 parameters has to be a required field.
This is how I am doing it.
10:45 $ python test....
1
vote
1
answer
53
views
Accept arbitrary arguments and options with Click
I'm writing a Python wrapper around another program. I want the user to be able to specify a few options for the wrapper and then pass the rest of the command-line through to the wrapped program. ...
0
votes
1
answer
90
views
Equivalent of sys.argv in click
I'm using click in Python to create a CLI for my program. I want to execute a function (below represented through the placeholder do_something()) if an exception occurs, but only if a certain argument ...
0
votes
1
answer
75
views
Issue with Dockerfile, Centos and Flask [duplicate]
I have the following Dockerfile:
FROM centos:centos7.9.2009
RUN yum update -y
RUN yum install -y python
RUN yum install -y python3-pip
RUN pip3 install flask
COPY app.py /opt/app.py
ENTRYPOINT ...
0
votes
1
answer
64
views
create structured data directly from python @click?
As discussed one may reuse python click decorators from several scripts easily.
However, with growing number of parameters
the main function parameter list gets crowded and voids pylint too-many-...
0
votes
2
answers
47
views
How to have nested command groups use the same name for the same level of subcommands but different implementation in click?
I am using click 8.0.3
I'm reading this https://click.palletsprojects.com/en/8.0.x/commands/#merging-multi-commands but I am still stumped.
What I want
i want to be able to have this
cli cli1 new
and
...
1
vote
1
answer
147
views
Python Poetry build not packaging cli file
I created this project with the following structure:
repo_dir
- module_name
- cli.py
- pyproject.toml
The cli.py file has code with functions using Click to call functions within the directory ...
0
votes
1
answer
82
views
Refreshing terminal session with Python and os.system()
I'm trying to write a cli tool that manages aliases and I want the script to be able to refresh the terminal session by running source ~/.zshrc or equivalent.
When I run os.system('source ~/.zshrc') ...
0
votes
2
answers
245
views
Is there a way to disable Click options from prompting via a global flag?
I'm using click to implement a command-line interface in Python. Click has a feature that can prompt for a value if one isn't specified. Like so:
@click.command()
@click.option('--name', prompt=True)
...
0
votes
1
answer
64
views
Python Click: How to make an option available only when another option has a certain value?
I want to enhance an existing command-line application done with Python click to allow for certain options depending on what the --format option is set to.
In my special case, I would like to enable --...
1
vote
1
answer
30
views
How to get one or none arguments with click?
Click allows for variadic arguments like this:
@click.command()
@click.argument('src', nargs=-1)
@click.argument('dst', nargs=1)
def copy(src, dst):
"""Move file SRC to DST."&...
1
vote
1
answer
115
views
Test click.prompt in an interactive session
I wonder how I can test consecutive click.prompt.
Questions regarding testing input prompts of python click seem to focus on one prompt only. One example focusses on one prompt given via command line.
...
2
votes
0
answers
41
views
Run click command from a given string
I have a sample python script that uses the click library:
import click
@click.group()
def cli():
pass
@cli.command()
@click.argument("name", default="User")
def hello(name):...
0
votes
1
answer
39
views
Error while using click module in python - Error: No such option: --addtask
Error - Error: No such option: --addtask
I ran code using - python3 main.py --addtask task
On running this addtask function should execute but it isnt
Saw several youtube videos still couldnt fix it
...
1
vote
1
answer
74
views
Python click incorrectly parses arguments when called in Vertex AI Pipeline
I'm trying to run a simple Ada-boosted Decision Tree regressor on GCP Vertex AI. To parse hyperparams and other arguments I use Click for Python, a very simple CLI library. Here's the setup for my ...
0
votes
2
answers
60
views
Using `click` in a shell script that has a Hy program in a here document
How can I convert the following working example of using click with shell + python repl (I think) to hy?
python3 - "$@" <<'EOF'
import click
@click.command()
@click.option('--count', ...
0
votes
0
answers
13
views
Can I disable the option type showing in python-click
Usage: main.py [OPTIONS]
Options:
-h, --host TEXT Database host address [default: localhost]
--port INTEGER Database host port [default: 1234]
-u, --user TEXT Database ...
0
votes
1
answer
42
views
Click unable to register group command
I am trying to run a click cli through a bash with a different command groups run through a single command collection.
src/preprocessing_extract_data/scripts/main.py
import click
@click.group()
def ...
0
votes
0
answers
29
views
How to invoke click.command with excluded arguments
I'm having a wrapper command that invokes certain command in a loop with period(int value, minutes)
Wrapper command doesn't want to describe arguments of command that is going to be wrapped.
@click....
0
votes
0
answers
20
views
how to use click to init a class, namely bind some click command to a __init__ of a specified class?
right now, I can do this to pass some default papameters to initialize a class:
@click.command()
@click.option("--project",
default = onnxfile,
help='project name'...
0
votes
1
answer
50
views
Proper way to test ClickException was raised with pytest?
I'm using Click to write a CLI and pytest to do testing. I have a command that generates an exception and throws a ClickException that I'd like to test:
@main.command(name="my-function")
def ...
0
votes
1
answer
86
views
Separate CLI commands over multiple Python files
I'm trying to package a command line interface, developed with the Click library. To experiment, I made another CLI test app with commands that do nothing to test things out.
My directory structure is ...
2
votes
0
answers
192
views
Can't start Uvicorn server from a Python Click Command
I am trying to create a click command that starts a uvicorn server but can't get the server to start.
I get a feeling it may be related to asyncio, which I am not very familiar with it.
Any help is ...
0
votes
1
answer
149
views
Click command default arguments and/or options
There are a few click commands I'd like to put the same argument(s) on. Normally, this would look like this:
@click.command()
@click.argument("arg", help="This is my argument")
def ...
0
votes
1
answer
195
views
Installing python packages locally doesn't always work
I'm creating a python 3.9 program and want to install packages locally. So the way my project is set up is this:
__main__.py
test.py
requirements.txt
lib/
__init__.py
In my requirements.txt file ...
3
votes
1
answer
83
views
Does Python Click prompt have any prefill options?
I'm trying to allow users to edit a parameter from a config file using Click. What I would like to do is present them with the previous parameter prefilled in a prompt input so that they can edit it ...
0
votes
0
answers
39
views
How to manually close file opened by class click.File within Python click function
I'm using the Python click CLI framework to write a CLI tool to transform some data, and then sort the output.
I would like to use click.File as input type so that I can automagically use - to write ...
0
votes
0
answers
24
views
Have Module Level Help Display Click Command Groups Without Subcommands
I have an application written that exposes a CLI via bunch of click command groups.
import click
@click.group()
def foo():
pass
@click.group():
def bar():
pass
@foo.command()
def some_foo():
...
0
votes
0
answers
119
views
Python Click - pass configuration file to supply both subcommands and options
I am trying to construct a Click MultiCommand group such that I'm able to supply a combination of options, subcommands and subcommand options, both from the commandline and from a configuration file.
...
0
votes
0
answers
59
views
Click: Add unspecified options to a pipeline
Question
I have a multi-command pipeline and would like to forward unknown options to it.
That is, I'd like to
python script.py --input "some input" --optionA valA --optionB valB mycommand
...
1
vote
1
answer
198
views
Python 3 + Click: CLI arguments get butchered when calling one command from another
I am using Python 3.9 and Click to build a small command line interface utility, but I am getting strange errors, specifically when I attempt to call one function decorated as a @click.command() from ...
1
vote
1
answer
88
views
Nested commands and help using click
Consider the example of a nested command using click (here):
# foo.py
@click.group()
@click.option("--debug/--no-debug", default=False)
@click.pass_context
def cli(ctx, debug):
# ...
0
votes
0
answers
18
views
Hi, I have a problem with runner.invoke() when test includes os.path.join() [duplicate]
I said before, I learning Click in python, and I can Units Testing, the problem is when I include os.path.join. that fail the test when the follow message
" assert result.exit_code == 0
E ...
1
vote
1
answer
211
views
How to 'self-invoke' python-click CLI commands without Context.invoke or Context.forward?
The click library discourages invoking the click.forward and click.forward functions to 'self-invoke' CLI commands. To quote the docs:
Sometimes, it might be interesting to invoke one command from ...
0
votes
0
answers
84
views
Problem when test-mocking a Choice argument in Python click
I wrote a small CLI example with python click.
import click
def get_list():
return ["a", "b"]
@click.command()
@click.argument("option", type=click.Choice(get_list()...
0
votes
1
answer
110
views
How to use 'Nuitka' to create a CLI app using a library like 'click'
When creating Click CLI apps we need to define entry points, which we do in setup.py
For example: (from click docs)
from setuptools import setup
setup(
name='yourscript',
version='0.1.0',
...
0
votes
1
answer
92
views
Can I check a subcommand can be run in Python Click before checking the options
I use python click to create a CLI with subcomands for my app. I've got a code like this:
@click.group()
def cli():
pass
@cli.command(debug)
@@click.option('--debug/--no-debug', default=False)
...