Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

python - Click: treat unknown subcommand as a parameter

In Python's Click package, is there any way to treat unknown subcommands as main script's arguments? Example: My script has two registered commands: init and ls, each accepts their own parameters. But if I want to call my script without those subcommands, and pass something else to it as the first parameter, I want some general (other) function to fire and process it.

# This would call `init` command with `location=some-location`
./my_script init some-location

# This would call `ls` command with `count=8`
./my_script ls --count 8

# But this should call a separate function with `item=item1`
./myscript item1

Essentially, the desired behaviour is the same as in password-store manager, where there are different commands, but calling pass with something other than a known command searches for a password.

I researched Click's documentation but did not find any ways to achieve it yet. I tried ignore_unknown_options or allow_extra_args, but could not make it work as I'd like.

question from:https://stackoverflow.com/questions/65938944/click-treat-unknown-subcommand-as-a-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...