Open victordomingos opened 4 years ago
Yes, this is a good feature. In the above example, the parser generates an error and system exit:
C:\Users\Net\Count-files>count-files --alfa
usage: count-files [-h] [-v] [-st] [-a] [-nr] [-c] [-nf] [-hc] [-t EXTENSION]
[-sf] [-ts] [-alpha] [-fe FILE_EXTENSION] [-fm PATTERN]
[-p] [-ps PREVIEW_SIZE] [-fs]
[path]
count-files: error: unrecognized arguments: --alfa
For error: unrecognized arguments
we may use https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_known_args.
print(parser.parse_known_args(*args)) <- Namespace here
['--alfa']
in the end - unknown argument
C:\Users\Net\Count-files>count-files --alfa
(Namespace(all=False, case_sensitive=False, extension=None, file_extension=None, file_sizes=False, help_cmd=False, no_feedback=False, no_recursion
=False, path='C:\\Users\\Net\\Count-files', pattern=None, preview=False, preview_size=730, show_folders=False, sort_alpha=False, supported_types=F
alse, total_size=False), ['--alfa'])
usage: count-files [-h] [-v] [-st] [-a] [-nr] [-c] [-nf] [-hc] [-t EXTENSION]
[-sf] [-ts] [-alpha] [-fe FILE_EXTENSION] [-fm PATTERN]
[-p] [-ps PREVIEW_SIZE] [-fs]
[path]
count-files: error: unrecognized arguments: --alfa
Here you need to think about how to check the arguments.
But this does not seem to work if the parser "considers" that the known argument was entered incorrectly:
C:\Users\Net\Count-files>count-files -alfa
usage: count-files [-h] [-v] [-st] [-a] [-nr] [-c] [-nf] [-hc] [-t EXTENSION]
[-sf] [-ts] [-alpha] [-fe FILE_EXTENSION] [-fm PATTERN]
[-p] [-ps PREVIEW_SIZE] [-fs]
[path]
count-files: error: argument -a/--all: ignored explicit argument 'lfa'
To avoid typos, we can also try to add the ability to create aliases.
But this option is more for long and repetitive commands.
alias table
= path/to/folder --sort-alpha --no-feedback --case-sensitive
This will probably be harder to do. And I don’t even know how to implement this :D
To avoid typos, we can also try to add the ability to create aliases. But this option is more for long and repetitive commands. alias
table
=path/to/folder --sort-alpha --no-feedback --case-sensitive
This will probably be harder to do. And I don’t even know how to implement this :D
Well, actually I don't see a great need for this, since the system shell usually takes care of tat kind of kind of feature, either by defining an alias, a variable, or even by writing a small script that replaces an otherwise longer and hard to type command. For instance, CMD.EXE
in Windows has the DOSKEY
command and, probably more used, .bat
scripts. PowerShell has New-Alias
and scripts. Bash has alias
and the configuration scripts, and so on…
I agree. I really don't want to write user settings support.
I am thinking on the kind of feedback we get when we mistype a
git
command:For instance, if a user types
count-files --alfa
instead of--alpha
, we could provide a nice suggestion indicating the correct spelling. For that purpose, we may find useful thedifflib.get_close_matches()
method from the Python Standard Library.