()
Process all cli parameters and call the search
Process all cli parameters and call the search
Process all cli parameters and call the search
def parse_params_and_search():
    '''
    Process all cli parameters and call the search
    '''
    parser = argparse.ArgumentParser(
        description='Search file name in directory tree'
    )

    env_var = bool(os.environ.get('FFIND_SEARCH_PATH'))
    parser.add_argument('-p',
                        action='store_true',
                        help='Match whole path, not only name of files. '
                             'Set env variable FFIND_SEARCH_PATH to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        dest='path_match',
                        # default False
                        default=env_var)

    env_var = bool(os.environ.get('FFIND_NO_COLOR'))
    parser.add_argument('--nocolor',
                        action='store_false',
                        dest='colored',
                        help='Do not display color. '
                             'Set env variable FFIND_NO_COLOR to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # default True
                        default=not env_var)

    env_var = bool(os.environ.get('FFIND_NO_SYMLINK'))
    parser.add_argument('--nosymlinks',
                        action='store_false',
                        dest='follow_symlinks',
                        help='Do not follow symlinks'
                             ' (following symlinks can lead to '
                             'infinite recursion) '
                             'Set env variable FFIND_NO_SYMLINK to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # default True
                        default=not env_var)

    env_var = bool(os.environ.get('FFIND_SEARCH_HIDDEN'))
    parser.add_argument('--hidden',
                        action='store_false',
                        dest='ignore_hidden',
                        help='Do not ignore hidden directories and files. '
                             'Set env variable FFIND_SEARCH_HIDDEN to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # default is True
                        default=not env_var)

    env_var = bool(os.environ.get('FFIND_CASE_SENSITIVE'))
    parser.add_argument('-c',
                        action='store_true',
                        dest='case_sensitive',
                        help='Force case sensitive. By default, all lowercase '
                             'patterns are case insensitive. '
                             'Set env variable FFIND_CASE_SENSITIVE to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # default is False
                        default=env_var)

    env_var = bool(os.environ.get('FFIND_CASE_INSENSITIVE'))
    parser.add_argument('-i',
                        action='store_true',
                        dest='case_insensitive',
                        help='Force case insensitive. This allows case '
                             'insensitive for patterns with uppercase. '
                             'If both -i and -c are set, the search will be '
                             'case sensitive.'
                             'Set env variable FFIND_CASE_INSENSITIVE to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # default is False
                        default=env_var)

    action = parser.add_mutually_exclusive_group()

    action.add_argument('--delete',
                        action='store_true',
                        dest='delete',
                        help='Delete files found',
                        default=False)

    action.add_argument('--exec',
                        dest='exec_command',
                        nargs=1,
                        metavar=('"command"'),
                        help='Execute the given command with the file found '
                             "as argument. The string '{}' will be replaced "
                             'with the current file name being processed. '
                             'If this option is used, ffind will return a '
                             'status code of 0 if all the executions return '
                             '0, and 1 otherwise',
                        default=False)

    action.add_argument('--module',
                        dest='run_module',
                        metavar=('"module_name args"'),
                        help='Execute the given module with the file found '
                             "as argument. The string '{}' will be replaced "
                             'with the current file name being processed. '
                             'If this option is used, ffind will return a '
                             'status code of 0 if all the executions return '
                             '0, and 1 otherwise.  Only SystemExit is caught',
                        default=False)

    action.add_argument('--command',
                        dest='program',
                        metavar=('"program"'),
                        help='Execute the given python program with the file '
                             "found placed in local variable 'filename'. "
                             'If this option is used, ffind will return a '
                             'status code of 1 if any exceptions occur, '
                             'and 0 otherwise.  SystemExit is not caught',
                        default=False)

    env_var = bool(os.environ.get('FFIND_IGNORE_VCS'))
    parser.add_argument('--ignore-vcs',
                        action='store_true',
                        dest='ignore_vcs',
                        help='Ignore version control system files and '
                             'directories. '
                             'Set env variable FFIND_IGNORE_VCS to set '
                             'this automatically'
                             '{0}'.format('[SET]' if env_var else ''),
                        # Default False
                        default=env_var)

    env_var = bool(os.environ.get('FFIND_FUZZY_SEARCH'))
    parser.add_argument('-f',
                        action='store_true',
                        dest='fuzzy',
                        help='Experimental fuzzy search. '
                             'Increases the matches, use with care. '
                             'Combining it with regex may give crazy results '
                             '{0}'.format('[SET]' if env_var else ''),
                        # Default False
                        default=env_var)

    parser.add_argument('--return-results',
                        action='store_true',
                        dest='return_results',
                        help='For testing purposes only. Please ignore',
                        default=False)

    parser.add_argument('--version', action='version',
                        version='%(prog)s {version}'.format(version=VERSION))

    parser.add_argument('dir', nargs='?',
                        help='Directory to search', default='.')
    parser.add_argument('filepattern')
    args = parser.parse_args()

    # If output is redirected, deactivate color
    if not sys.stdout.isatty():
        args.colored = False

    lowercase_pattern = args.filepattern == args.filepattern.lower()
    if args.case_sensitive:
        ignore_case = False
    elif args.case_insensitive:
        ignore_case = True
    else:
        ignore_case = lowercase_pattern

    exec_result = search(directory=args.dir,
                         file_pattern=args.filepattern,
                         path_match=args.path_match,
                         output=True,
                         colored=args.colored,
                         follow_symlinks=args.follow_symlinks,
                         ignore_hidden=args.ignore_hidden,
                         delete=args.delete,
                         ignore_case=ignore_case,
                         exec_command=args.exec_command,
                         return_exec_result=True,
                         ignore_vcs=args.ignore_vcs,
                         fuzzy=args.fuzzy,
                         return_results=args.return_results,
                         run_module_command=args.run_module,
                         program=args.program)

    if args.return_results:
        exit(0)

    exit(exec_result)
def parse_params_and_search ( ) : parser = argparse . ArgumentParser ( description = 'Search file name in directory tree' ) env_var = bool ( os . environ . get ( 'FFIND_SEARCH_PATH' ) ) parser . add_argument ( '-p' , action = 'store_true' , help = 'Match whole path, not only name of files. ' 'Set env variable FFIND_SEARCH_PATH to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , dest = 'path_match' , # default False default = env_var ) env_var = bool ( os . environ . get ( 'FFIND_NO_COLOR' ) ) parser . add_argument ( '--nocolor' , action = 'store_false' , dest = 'colored' , help = 'Do not display color. ' 'Set env variable FFIND_NO_COLOR to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # default True default = not env_var ) env_var = bool ( os . environ . get ( 'FFIND_NO_SYMLINK' ) ) parser . add_argument ( '--nosymlinks' , action = 'store_false' , dest = 'follow_symlinks' , help = 'Do not follow symlinks' ' (following symlinks can lead to ' 'infinite recursion) ' 'Set env variable FFIND_NO_SYMLINK to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # default True default = not env_var ) env_var = bool ( os . environ . get ( 'FFIND_SEARCH_HIDDEN' ) ) parser . add_argument ( '--hidden' , action = 'store_false' , dest = 'ignore_hidden' , help = 'Do not ignore hidden directories and files. ' 'Set env variable FFIND_SEARCH_HIDDEN to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # default is True default = not env_var ) env_var = bool ( os . environ . get ( 'FFIND_CASE_SENSITIVE' ) ) parser . add_argument ( '-c' , action = 'store_true' , dest = 'case_sensitive' , help = 'Force case sensitive. By default, all lowercase ' 'patterns are case insensitive. ' 'Set env variable FFIND_CASE_SENSITIVE to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # default is False default = env_var ) env_var = bool ( os . environ . get ( 'FFIND_CASE_INSENSITIVE' ) ) parser . add_argument ( '-i' , action = 'store_true' , dest = 'case_insensitive' , help = 'Force case insensitive. This allows case ' 'insensitive for patterns with uppercase. ' 'If both -i and -c are set, the search will be ' 'case sensitive.' 'Set env variable FFIND_CASE_INSENSITIVE to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # default is False default = env_var ) action = parser . add_mutually_exclusive_group ( ) action . add_argument ( '--delete' , action = 'store_true' , dest = 'delete' , help = 'Delete files found' , default = False ) action . add_argument ( '--exec' , dest = 'exec_command' , nargs = 1 , metavar = ( '"command"' ) , help = 'Execute the given command with the file found ' "as argument. The string '{}' will be replaced " 'with the current file name being processed. ' 'If this option is used, ffind will return a ' 'status code of 0 if all the executions return ' '0, and 1 otherwise' , default = False ) action . add_argument ( '--module' , dest = 'run_module' , metavar = ( '"module_name args"' ) , help = 'Execute the given module with the file found ' "as argument. The string '{}' will be replaced " 'with the current file name being processed. ' 'If this option is used, ffind will return a ' 'status code of 0 if all the executions return ' '0, and 1 otherwise.  Only SystemExit is caught' , default = False ) action . add_argument ( '--command' , dest = 'program' , metavar = ( '"program"' ) , help = 'Execute the given python program with the file ' "found placed in local variable 'filename'. " 'If this option is used, ffind will return a ' 'status code of 1 if any exceptions occur, ' 'and 0 otherwise.  SystemExit is not caught' , default = False ) env_var = bool ( os . environ . get ( 'FFIND_IGNORE_VCS' ) ) parser . add_argument ( '--ignore-vcs' , action = 'store_true' , dest = 'ignore_vcs' , help = 'Ignore version control system files and ' 'directories. ' 'Set env variable FFIND_IGNORE_VCS to set ' 'this automatically' '{0}' . format ( '[SET]' if env_var else '' ) , # Default False default = env_var ) env_var = bool ( os . environ . get ( 'FFIND_FUZZY_SEARCH' ) ) parser . add_argument ( '-f' , action = 'store_true' , dest = 'fuzzy' , help = 'Experimental fuzzy search. ' 'Increases the matches, use with care. ' 'Combining it with regex may give crazy results ' '{0}' . format ( '[SET]' if env_var else '' ) , # Default False default = env_var ) parser . add_argument ( '--return-results' , action = 'store_true' , dest = 'return_results' , help = 'For testing purposes only. Please ignore' , default = False ) parser . add_argument ( '--version' , action = 'version' , version = '%(prog)s {version}' . format ( version = VERSION ) ) parser . add_argument ( 'dir' , nargs = '?' , help = 'Directory to search' , default = '.' ) parser . add_argument ( 'filepattern' ) args = parser . parse_args ( ) # If output is redirected, deactivate color if not sys . stdout . isatty ( ) : args . colored = False lowercase_pattern = args . filepattern == args . filepattern . lower ( ) if args . case_sensitive : ignore_case = False elif args . case_insensitive : ignore_case = True else : ignore_case = lowercase_pattern exec_result = search ( directory = args . dir , file_pattern = args . filepattern , path_match = args . path_match , output = True , colored = args . colored , follow_symlinks = args . follow_symlinks , ignore_hidden = args . ignore_hidden , delete = args . delete , ignore_case = ignore_case , exec_command = args . exec_command , return_exec_result = True , ignore_vcs = args . ignore_vcs , fuzzy = args . fuzzy , return_results = args . return_results , run_module_command = args . run_module , program = args . program ) if args . return_results : exit ( 0 ) exit ( exec_result )