Closed NataliaBondarenko closed 5 years ago
I propose to delete the total
as a separate group.
Make the argument -t, --total
(bool) part of the search group.
count-files --file-extension txt --total <common arguments>
In this case, only the quantity will be displayed.
We already have a lot of arguments.
I was thinking about how to make a more user-friendly interface.
Some parser groups use the same function: def search_files
.
And they can also display results in a similar way, using def show_result_for_search_files
We can also make a boolean argument --total
. And use def show_result_for_total
in this case to display the results.
I propose to transform the structure of the parser (in the part of the search and listing of paths) as follows:
main arg | return | related args |
---|---|---|
count (no changes) | ||
defaulf | all extensions and frequency | --alpha |
search | ||
--file-extension [substring] | extensions that startswith*, *endswith, *contain* or exact match the substring | --preview, --preview-size, --file-sizes, --total(bool) |
--file-name [substring] | filenames that startswith*, *endswith, *contain* or exact match the substring | --preview, --preview-size, --file-sizes, --total(bool) |
--file-path [substring] | filepaths that startswith*, *endswith, *contain* or exact match the substring | --preview, --preview-size, --file-sizes, --total(bool) |
There is another option:
main arg | return | switch args(boolean) | related args |
---|---|---|---|
count (no changes) | |||
defaulf | all extensions and frequency | none | --alpha |
search | |||
--search [substring] | results that startswith*, *endswith, *contain* or exact match the substring | --file-extension(mean search in extensions), --file-name(mean search in filenames), --file-path(mean search in whole paths) | --preview, --preview-size, --file-sizes, --total(bool) |
I think it will be easier to use. What do you think about it?
P.S. In addition, I had an idea, if using only count-files
with no arguments, so that the parser would show help text.
Help can be made interactive, similar to the built-in help()
.
This can be implemented using python cmd
https://docs.python.org/3/library/cmd.html
In this case, the user can type less to find the desired argument or description.
Rewrite the Cmd.default()
method to find a specific word in the indexes
.
Then instead of count-files --args-help file-extension
you can run help cmd and type only the names of the arguments you need.
main arg | return |
---|---|
default | |
no arguments | start help cmd(equivalent to --args-help) |
count | |
--count | all extensions and frequency |
search | |
--search [substring] | results that startswith*, *endswith, *contain* or exact match the substring |
Try it:
python filename.py
or
python filename.py -h
import cmd
from count_files.utils.help_system_extension import search_in_help
class HelpCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.intro = 'Welcome to Count Files Help! Type "help" or "?" to list cmd commands.\n' \
'To quit this utility, just type "quit".\n'
self.prompt = 'help> '
def do_docs(self, arg):
"""More about help cmd usage."""
print('Corresponding text.')
def do_list(self, arg):
"""Get a list of available topics for searching or sorting."""
print('Corresponding text.')
# and so on...
def do_quit(self, arg):
"""Exit the help cmd by entering "quit"."""
print('Exit the cmd.')
return True
def emptyline(self):
"""Method called when an empty line is entered in response to the prompt."""
print('Nothing entered, empty line.')
def default(self, arg):
search_in_help(arg)
if __name__ == '__main__':
import argparse
import os
parser = argparse.ArgumentParser(description='Does some important things.', add_help=False)
parser.add_argument('-h', '--help', action='store_true', default=False,
help='Start interactive Help.')
parser.add_argument('--count', action='store_true', default=False,
help='Count group.')
parser.add_argument('--search', type=str,
help='Search group.')
parser.add_argument('path', nargs='?', default=os.getcwd(), type=str,
help='Path.')
args = parser.parse_args()
# 1) start with argument, like --args-help now
if args.help:
hc = HelpCmd()
hc.cmdloop()
parser.exit(0)
if args.count:
# if args do something
print('Count')
print(args.path)
elif args.search:
# if args do something
print('Search')
print(args.path)
else:
# 2) else start default help
hc = HelpCmd()
hc.cmdloop()
I like the idea of an interactive help system. What I am not so sure about is if it is a good idea to change the user interface for the CLI arguments. I mean, until now we had a simple command “count-files” that would right away count the files in current directory. While I understand that it limits a little what we can do with optional arguments, I don’t want to loose the simplicity of that basic command.
Another option we may consider is to split it in two executable entrypoints, each one with its own set of CLI arguments:
count-files
search-files
Find group: displays the location of the files matching the search pattern (list).
--path-contains
--filename-contains
--extension-contains
Arguments--path-contains
,--filename-contains
and--extension-contains
are used separately from each other. Also--preview
,--preview-size
,--file-sizes
arguments are available for this group. Service argument:--find-patterns
- short info about usage Available patterns:substring*
,*substring
,*substring*
orsubstring
(startswith, endswith, contains).*
- substitute character (mask "any number of any characters") substring: any character or word to check if the path, filename, extnsion contains it.Also: