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

Find group: search in pahs, extensions, filenames(excluding extension). Contains, startswith, endswith. #105

Closed NataliaBondarenko closed 5 years ago

NataliaBondarenko commented 5 years ago

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* or substring (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:

NataliaBondarenko commented 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.

NataliaBondarenko commented 5 years ago

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 argreturnrelated args
count (no changes)
defaulfall 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 argreturnswitch args(boolean)related args
count (no changes)
defaulfall extensions and frequencynone--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?

NataliaBondarenko commented 5 years ago

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 argreturn
default
no argumentsstart help cmd(equivalent to --args-help)
count
--countall extensions and frequency
search
--search [substring]results that startswith*, *endswith, *contain* or exact match the substring
NataliaBondarenko commented 5 years ago

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()
victordomingos commented 5 years ago

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