1111
1212import os
1313import sys
14-
14+ import argparse
1515
1616def batch_rename (work_dir , old_ext , new_ext ):
1717 '''
@@ -32,17 +32,27 @@ def batch_rename(work_dir, old_ext, new_ext):
3232 os .path .join (work_dir , newfile )
3333 )
3434
35+ def get_parser ():
36+ parser = argparse .ArgumentParser (description = 'change extension of files in a working directory' )
37+ parser .add_argument ('work_dir' , metavar = 'WORK_DIR' , type = str , nargs = 1 , help = 'the directory where to change extension' )
38+ parser .add_argument ('old_ext' , metavar = 'OLD_EXT' , type = str , nargs = 1 , help = 'old extension' )
39+ parser .add_argument ('new_ext' , metavar = 'NEW_EXT' , type = str , nargs = 1 , help = 'new extension' )
40+ return parser
3541
3642def main ():
3743 '''
3844 This will be called if the script is directly invoked.
3945 '''
46+ # adding command line argument
47+ parser = get_parser ()
48+ args = vars (parser .parse_args ())
49+
4050 # Set the variable work_dir with the first argument passed
41- work_dir = sys . argv [ 1 ]
51+ work_dir = args [ 'work_dir' ][ 0 ]
4252 # Set the variable old_ext with the second argument passed
43- old_ext = sys . argv [ 2 ]
53+ old_ext = args [ 'old_ext' ][ 0 ]
4454 # Set the variable new_ext with the third argument passed
45- new_ext = sys . argv [ 3 ]
55+ new_ext = args [ 'new_ext' ][ 0 ]
4656
4757 batch_rename (work_dir , old_ext , new_ext )
4858
0 commit comments