ajax
ajax

Reputation: 11

command line argument followed by argparse option

I'm new to python and currently playing around with argpase. I'm trying to call a function using a directory path given as a command line argument followed by an argparse option(-name) and a regex that goes through all the files in the directory and spits out all the matches to the regex as so:

./find.py ../seek -name '[a-z]*\.txt'

However, I'm getting a error that looks like

usage: find.py [-h] [--path PATH] [-name] [--regex REGEX] find.py: error: unrecognized arguments: . . / s e e k / p r o g r a m . c

And without the -name its just printing all the files inside the path.

Here is what I have so far:

#!/usr/bin/python2.7

import os, sys, argparse,re 
from stat import *

def parse(argv=None):
   parser = argparse.ArgumentParser()
   parser.add_argument('--path', help='path of directory', action='store')
   parser.add_argument('-name', '--name', action='store_true')
   parser.add_argument('--regex', default=r"[a-z0-9A-Z]")
   args = parser.parse_args(argv)
   print(args)
   return args

def main(argv=None):

   direc = sys.argv[1]
   files = []

   for f in os.listdir(direc):
      pathname = os.path.join(direc, f)
      mode = os.stat(pathname).st_mode

      if S_ISREG(mode):  
         args = parse(pathname)      
         if args.name:  
            dirls = [re.match(args.regex, pathname)]
            print(dirls)
         else:
            print pathname

if __name__ == '__main__':main() 

Any thoughts?

Upvotes: 0

Views: 785

Answers (2)

Asad Manzoor
Asad Manzoor

Reputation: 1465

Argument Parser PATH Example : Different type of arguments with custom handlers added. For path here you can pass '-path' followed by path value as argument

import os
import argparse
from datetime import datetime


def parse_arguments():
    parser = argparse.ArgumentParser(description='Process command line arguments.')
    parser.add_argument('-path', type=dir_path)
    parser.add_argument('-e', '--yearly', nargs = '*', help='yearly date', type=date_year)
    parser.add_argument('-a', '--monthly', nargs = '*',help='monthly date', type=date_month)

    return parser.parse_args()


def dir_path(path):
    if os.path.isdir(path):
    return path
    else:
    raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")


def date_year(date):
    if not date:
    return

    try:
    return datetime.strptime(date, '%Y')
    except ValueError:
    raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")


def date_month(date):
    if not date:
    return

    try:
    return datetime.strptime(date, '%Y/%m')
    except ValueError:
    raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")


def main():
    parsed_args = parse_arguments()

if __name__ == "__main__":
main()

Upvotes: 1

J...S
J...S

Reputation: 5207

In order for your program to operate, you need a path. So, the --path option must take an argument. Modify your parse() function to change the line

parser.add_argument('--path', help='path of directory', action='store')

to

parser.add_argument('--path', help='path of directory', action='store', required=True)

You need to call parse_args() only once. Remove the parse() invocation to the top of the loop.

And you needn't do

direc = sys.argv[1]

if you are using argparse.

re.match() returns a match object, which is probably not what you want to print.

You might want to take a look at match() versus search().

The match() function only checks if the RE matches at the beginning of the string while search() will scan forward through the string for a match.

If you wanted to print the file names matching the regex, you could do

  if S_ISREG(mode):  
     #args = parse(pathname)      
     if args.name:  
        #dirls = re.match(args.regex, pathname)
        dirls = re.search(args.regex, pathname)
        if( dirls ):
           print(pathname)
     else:
        print pathname

So main() should be something like

def main(argv=None):
    args = parse(sys.argv[1:])
    print(args)
    #direc = sys.argv[1]
    direc = args.path
    files = []

    for f in os.listdir(direc):
      pathname = os.path.join(direc, f)
      mode = os.stat(pathname).st_mode

    if S_ISREG(mode):  
       #args = parse(pathname)      
       if args.name:  
          #dirls = re.match(args.regex, pathname)
          dirls = re.search(args.regex, pathname)
          if( dirls ):
             print(pathname)
       else:
          print pathname

In order to specify the regex matching the file names, you must specify the regex using the --regex option. By default, you've made it to match names having only numbers and (English) letters.

./find.py --path ../seek -name --regex [a-z]\*.txt

or

./find.py --path ../seek -name --regex '[a-z]*.txt'

Upvotes: 0

Related Questions