victordomingos / Count-files

A CLI utility written in Python to help you count files, grouped by extension, in a directory. By default, it will count files recursively in current working directory and all of its subdirectories, and will display a table showing the frequency for each file extension (e.g.: .txt, .py, .html, .css) and the total number of files found.
https://no-title.victordomingos.com/projects/count-files/
MIT License
23 stars 9 forks source link

Suggest the most similar command(s) when a CLI argument is not recognised #112

Open victordomingos opened 4 years ago

victordomingos commented 4 years ago

I am thinking on the kind of feedback we get when we mistype a git command:

$ git sstat
git: 'sstat' não é um comando do git. Consulte 'git --help'.

The most similar command is
    status

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 the difflib.get_close_matches() method from the Python Standard Library.

NataliaBondarenko commented 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'
NataliaBondarenko commented 4 years ago

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

victordomingos commented 4 years ago

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…

NataliaBondarenko commented 4 years ago

I agree. I really don't want to write user settings support.