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 to Click 8.x and have the literal string "*.py" show up in the filepattern variable.
Click 8.x expands the glob patterns by default (this is different than 7.x). On Bash, if you quote the glob pattern, the shell doesn't do the expansion, and Click passes in the string. On Windows, the shell doesn't do the expansion and passes the string into Python. In this case Click expands them.
So the question here is how do I get the string "*.py" passed in as a string on Windows (if there are OR are not any files that match)? I only want to allow 1 argument (so nargs=1). This all works fine in Click 7.x.
The following example saved as mycommand.py:
import click
@click.command("mycommand")
@click.argument("filepattern", nargs=1, type=str)
def mycommand(filepattern):
print(filepattern)
if __name__ == "__main__":
mycommand()
If I have a directory full of, say Python files, if I invoke this as python mycommand.py somefile.py, it will succeed as one value gets passed into filepattern and it will echo somefile.py.
If I invoke as python mycommand.py *.py it fails with an error like:
Usage: mycommand.py [OPTIONS] FILEPATTERN
Try 'mycommand.py --help' for help.
Error: Got unexpected extra arguments (scratch.py mycommand.py ...)
I know there is an argument windows_expand_args for a click.group, but I can't puzzle out how to get it to work for a single command program.
python mycommand.py *.pyon Linux I got same error.