557 questions
-1
votes
0
answers
51
views
Use fallback subcommand if no subcommand matches [duplicate]
I have a tool that uses Click to define its CLI interface. It uses groups to achieve a subcommand hierarchy. So, if it is mytool, we have commands like mytool edit, mytool show, mytool execute script1....
0
votes
1
answer
33
views
click.option with deprecated=str failed with unexpected keyword argument
Here is short sample, which uses click==8.1.7
# main.py
import click
@click.command
@click.option(
"--disable-all",
is_flag=True,
default=False,
deprecated="Please use --...
0
votes
0
answers
100
views
Python Click Shell Completion
i am currently trying to get a compiled python project which is using click 8.0.4 running with shell_completion.
My project contains multiple downstream scripts, which should support the ...
2
votes
1
answer
152
views
Click help text shows 'dynamic' when an option has the default set to a lambda
I have this code in a CLI:
@click.option(
"--username",
default=lambda: os.environ.get("USER", None),
show_default=True,
help="User name for SSH configuration.&...
0
votes
0
answers
82
views
Define a dynamic default value of an option based on another option when using Typer
I have the following ~minimal code:
from typing import Annotated
import typer
def main(
username: Annotated[
str,
typer.Option("--username", "-u", help="...
0
votes
0
answers
33
views
Python click CLI subcommand multiple times
I am new to the click CLI library, so this might be something trivial.
I have a script which has one group and one subcommand, something like this. It is a ML application which has a training phase ...
1
vote
1
answer
77
views
Click: How to propagate the exit code back to the group
I have a script which use click.group to provide sub commands. Each of the sub command might pass or fail. How do I propagate the exit code from the sub commands back to main?
import click
import sys
...
4
votes
1
answer
49
views
Get Click to not expand variables in argument
I have a simple Click app like this:
import click
@click.command()
@click.argument('message')
def main(message: str):
click.echo(message)
if __name__ == '__main__':
main()
When you pass an ...
1
vote
1
answer
121
views
How to force Python Click to emit colors when writing to a pipe?
When click.secho("helo", fg="yellow") writes to a pipe, the colors are stripped by default. If there a way to force click to emit the color codes even when stdout is a pipe?
...
0
votes
0
answers
68
views
How to create a terminal-like python pipe?
Some programs emit colored text when their output is connected to a terminal, and non colored text when their output goes to a pipe.
Is there a way in Python to open a pipe to a subprocess such that ...
0
votes
2
answers
184
views
How to override the help text of a shared python click option?
I am using python click options that are shared by multiple commands, as described at https://stackoverflow.com/a/77732441.
Is there a simple way to customize the help= text of the list option in the ...
2
votes
1
answer
234
views
How to use windows_expand_args with single command click program
I have a Python program and am using Click to process the command line. I have a single command program, as in the example below. On Windows I would like to pass in a valid glob expression like *.py ...
1
vote
1
answer
161
views
How to use Python Click's `ctx.with_resource` to capture tracebacks in (sub-)commands/groups
In Click, Context.with_resource's documentation states it can be used to:
[r]egister a resource as if it were used in a with statement. The resource will be cleaned up when the context is popped.
...
2
votes
1
answer
123
views
How do I know if flag was passed by the user or has default value?
Sample code:
import click
@click.command
@click.option('-f/-F', 'var', default=True)
def main(var):
click.echo(var)
main()
Inside main() function, how can I check if var parameter got True value ...
2
votes
1
answer
398
views
Changing command order in Python's Typer
I want Typer to display my commands in the order I have initialized them and it displays those commands in alphabetic order.
I have tried different approaches including this one: https://github.com/...
0
votes
1
answer
68
views
Keep python click argument case
Using the following click application:
from rich.traceback import install
install(show_locals=True)
import click
@click.command()
@click.argument("PATH", envvar="PATH", nargs=-1, ...
0
votes
1
answer
336
views
Unable to read file with click.Path option inside runner.isolated_filesystem()
I am using click for the very first time to create a cli program using Python 3.12.2.
I created a simple click command that takes an option (the '-f' / '--file' option) as a filepath with type click....
1
vote
1
answer
363
views
I Get "TypeError: 'NoneType' object is not callable" Error on "@main.result_callback()" When executing run.sh for SimpleNet
I am trying yo run SimpleNet network and when I enter the command bash run.sh on the terminal I see the following output:
Matplotlib created a temporary cache directory at /tmp/matplotlib-ovjj_rt5 ...
1
vote
0
answers
90
views
Using a '=' as a trigger for click shell completion in my python click application
Intro
It is a strange thing that in pallets click module options can parse a command option in the '--option=value' format, where value is a string, and that is equivalent to '--option value' where ...
2
votes
2
answers
689
views
Google Cloud Vertex AI: custom-job cannot specify arg multiple times error
I have a Python project using click args where an argument should be in tuple(string,float) format and can accept multiple instances of the arg.
@click.option(
"--metric-and-lift",
...
1
vote
2
answers
503
views
apscheduler BackgroundScheduler() process is not running in the background
I am working on a project.
Below are the files from which the problem resides.
cli.py
import click
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler(...
0
votes
1
answer
337
views
how to detect if input is stdin or not using python click?
Consider the following code:
import click
@click.command()
@click.argument("file", type=click.File())
def cli(file):
print(file)
if __name__ == "__main__":
cli()
...
-1
votes
1
answer
465
views
Passing Python click options to an ENTRYPOINT using docker run gives error: "executable file not found in $PATH" [closed]
I have a simple python script that I want to run inside of a docker container. It prints a one-line message "Hello {name}". The python script uses the click CLI interface to define the ...
0
votes
1
answer
164
views
Get unparsed parameters in click application
In click application from Context we can get command name, its full path and parsed parameters like below. Can we get unparsed parameters from click?
Edit: updated question according to @julaine and @...
1
vote
0
answers
105
views
How can I use python-click to make a multi-command CLI interface
I'd like to create a CLI for my application that's flexible and extensible. Click seems like the right solution but I'm new to it and have not seen an example that fits what I'm looking for. The ...
2
votes
1
answer
456
views
How to set python click parameter to alias different parameter from option callback?
There are many utilities that have one option to be an alias for others, like the following:
$ python3 ./1.py --help
Usage: 1.py [OPTIONS]
Options:
--format TEXT
--long TEXT Alias to --format=...
0
votes
0
answers
270
views
How to patch a click cli app for unit testing
I have a cli tool written in Python using Click:
cli/main.py
import click
from datetime import *
from .utils import fn
@click.group(invoke_without_command=True, context_settings = { '...
0
votes
1
answer
170
views
Multi lines Click epilog from a file
I'm developping a large Python3 application, with Click to handle parameters.
Some actions have many scenarios, and I describe them in the epilog.
To keep the script clean, I would like to put epilogs ...
3
votes
1
answer
301
views
Creating a wrapper decorator for click.options()
I am trying to create a wrapper decorator for the click decorator @click.options('--foo', required=True):
import click
def foo_option(func):
orig_decorator = click.option('--foo', required=True)(...
2
votes
1
answer
74
views
Refactor @click.option() arguments
Say I have this (minimal example from a larger script):
import click
@click.command()
@click.option('--foo', required=True)
def bar1(foo: str) -> None:
print(f"bar1: {foo}")
@click....
0
votes
0
answers
201
views
How can I get a callback to be run even on the event of an error in the python click framework?
I have a piece of code which I wish to always run at the end of a command execution, regardless of whether the command succeeded or raised an exception. I thought callbacks may be used for this, but ...
-1
votes
1
answer
196
views
How do I manually invoke click CLIs from within a Python calling context with parameters?
In order to set up automatic function aliases in setuptools, it's necessary to wrap all args in some kind of function. If I am using a click interface, I'll need some way to parameterize specific ...
0
votes
0
answers
70
views
Args to be optional when an option is used, else required
@cli.command()
@click.option("--list", 'list_steps', default=False, is_flag=True)
@click.argument("elem")
@click.pass_obj
def step(cmd, elem, list_steps):
print(elem)
print(...
1
vote
2
answers
769
views
How to deal with a Windows path given as an argument
I have a script that takes a file path as an argument and I'm having issues with how to handle Windows paths in this situation. How do I avoid a situation like this "T:\not\a\good\path" ...
1
vote
1
answer
451
views
How to pass a JSON file as an argument to Python click
I'm using click to build a CLI tool. I need my code to open a file in a specified directory (this is what is passed as a command line argument) and parse the json inside that file. What is the best ...
0
votes
0
answers
187
views
Why click converts all arguments into lowercase
I would like to make an UPPER_CASE_PARAMETER into an argument to the function which is named in the same way.
As I understand the convention is that all arguments to the function are variables and ...
1
vote
0
answers
36
views
Unit Test for Python Click Command Fails on macOS Due to Sandbox Path Difference
I have a Python module that uses the Click library for command-line interfaces.
Assume the module defines a foo function that takes a file path as an argument and calls the bar function with the ...
0
votes
1
answer
44
views
Click package: dynamical prompt message for command's option
I'm using the Python package Click to write a user interface for my code. Basically, I want to define a command with a prompt message that is defined during the running of the code, i.e. that depends ...
3
votes
1
answer
3k
views
click.Path sometimes parsed as bytes instead of pathlib.Path
I sometimes have the issue that the click package fails to return a Path when using the click.Path argument, and instead I get a bytes object. Here is a typical code sample which will sometimes cause ...
0
votes
1
answer
72
views
How to read two files at once with Python Click
I am trying to use Click CLI package to read two files at once. Is it possible in main function to have two options for file paths?
import numpy as np
import pandas as pd
import click
import sys
...
0
votes
1
answer
104
views
CLI command with Click package Python [closed]
I am using click library in order to call function from one folder and read file and after that to write file.
import sys
import numpy as np
import pandas as pd
import click
def count_unique_port(df:...
0
votes
1
answer
195
views
How can I read the rest of a command line into the last argument in Python Click, ignoring existing options?
I want to read the rest of the line into the last argument ignoring that it contains even existing options (as the script that I am going to run may contain the same options as the main script ...
0
votes
1
answer
198
views
How to run a command line app continuously until instructed to quit
I'm using Python and Click to create a simple command line app that takes in a number and adds it to a default number. This is what I have so far:
import click
from tqdm import tqdm
init = 5
for i in ...
0
votes
0
answers
149
views
Flask custom command not found in a docker container
I'm running a simple Flask app in docker container and i wrote a custom command that would help creating superuser in the postgres table.
The custom flask command snippet
app = Flask(__name__)
api = ...
1
vote
1
answer
213
views
Launch Click CLI Application from inside python code
I would like to integrate a python CLI written using click into a python CLI application built using another framework. Rewriting the calling application is not an option.
As part of the integration, ...
1
vote
0
answers
42
views
How do I enable autocompletion for windows users?
I implemented a CLI tool using click and it has a way for linux users to have autocompletion as it's nicely documented. Basically one adds source $(_FOO_COMPLETE=bash_source foo) to ~/.bashrc
Now, one ...
1
vote
1
answer
723
views
python click custom shell completion for command
I have a click application which is running just fine currently.
We want to introduce a new command called api which will dynamically reach into our python sdk and extract sdk methods that can be ...
0
votes
1
answer
297
views
Bash completion not working after upgrading click version from version 7.0 to 8.1.3
Bash completion is working normally with Click 7.0
Environment:
Python version: 3.9.9
Click version: 7.0
Bash version: 5.1.8
Bash-completion:2.11
This is the code:
import click
@click.group(cls=...
1
vote
0
answers
850
views
Invoke an HFArgumentParser based script from within a Click command
I have a script trainmodel.py coming from an existing codebase (based on Hugginface ArgumentParsers) with almost 100 different arguments that is based on argparse. The script comes as a main() ...
1
vote
1
answer
938
views
Why this asyncio code doesn't run concurrent?
Hello I am trying to write a script to process pdf files with asyncio concurrent in order to do this I have the following code:
import click
import asyncio
from pdf2image import convert_from_path
from ...