3131 % bagit.py --help
3232"""
3333
34+ import argparse
3435import codecs
3536import hashlib
3637import logging
3738import multiprocessing
38- import optparse
3939import os
4040import re
4141import signal
@@ -876,42 +876,45 @@ def _decode_filename(s):
876876
877877# following code is used for command line program
878878
879- class BagOptionParser ( optparse . OptionParser ):
880- def __init__ (self , * args , ** opts ):
879+ class BagArgumentParser ( argparse . ArgumentParser ):
880+ def __init__ (self , * args , ** kwargs ):
881881 self .bag_info = {}
882- optparse . OptionParser .__init__ (self , * args , ** opts )
882+ argparse . ArgumentParser .__init__ (self , * args , ** kwargs )
883883
884884
885- def _bag_info_store (_ , opt , value , parser ):
886- opt = opt .lstrip ('--' )
887- opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
888- parser .bag_info [opt_caps ] = value
885+ class BagHeaderAction (argparse .Action ):
886+ def __call__ (self , parser , _ , values , option_string = None ):
887+ opt = option_string .lstrip ('--' )
888+ opt_caps = '-' .join ([o .capitalize () for o in opt .split ('-' )])
889+ parser .bag_info [opt_caps ] = values
889890
890891
891- def _make_opt_parser ():
892- parser = BagOptionParser (usage = 'usage: %prog [options] dir1 dir2 ...' )
893- parser .add_option ('--processes' , action = 'store' , type = "int" ,
894- dest = 'processes' , default = 1 ,
892+ def _make_parser ():
893+ parser = BagArgumentParser ()
894+ parser .add_argument ('--processes' , type = int , dest = 'processes' , default = 1 ,
895895 help = 'parallelize checksums generation and verification' )
896- parser .add_option ('--log' , action = 'store' , dest = ' log' , help = 'The name of the log file' )
897- parser .add_option ('--quiet' , action = 'store_true' , dest = 'quiet ' )
898- parser .add_option ('--validate' , action = 'store_true' , dest = 'validate ' )
899- parser .add_option ('--fast' , action = 'store_true' , dest = 'fast ' )
896+ parser .add_argument ('--log' , help = 'The name of the log file' )
897+ parser .add_argument ('--quiet' , action = 'store_true' )
898+ parser .add_argument ('--validate' , action = 'store_true' )
899+ parser .add_argument ('--fast' , action = 'store_true' )
900900
901901 # optionally specify which checksum algorithm(s) to use when creating a bag
902902 # NOTE: could generate from checksum_algos ?
903- parser .add_option ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
903+ parser .add_argument ('--md5' , action = 'append_const' , dest = 'checksum' , const = 'md5' ,
904904 help = 'Generate MD5 manifest when creating a bag (default)' )
905- parser .add_option ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
905+ parser .add_argument ('--sha1' , action = 'append_const' , dest = 'checksum' , const = 'sha1' ,
906906 help = 'Generate SHA1 manifest when creating a bag' )
907- parser .add_option ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
907+ parser .add_argument ('--sha256' , action = 'append_const' , dest = 'checksum' , const = 'sha256' ,
908908 help = 'Generate SHA-256 manifest when creating a bag' )
909- parser .add_option ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
909+ parser .add_argument ('--sha512' , action = 'append_const' , dest = 'checksum' , const = 'sha512' ,
910910 help = 'Generate SHA-512 manifest when creating a bag' )
911911
912912 for header in STANDARD_BAG_INFO_HEADERS :
913- parser .add_option ('--%s' % header .lower (), type = "string" ,
914- action = 'callback' , callback = _bag_info_store )
913+ parser .add_argument ('--%s' % header .lower (), type = str ,
914+ action = BagHeaderAction )
915+
916+ parser .add_argument ('directory' , nargs = '+' , help = 'directory to make a bag from' )
917+
915918 return parser
916919
917920
@@ -928,24 +931,24 @@ def _configure_logging(opts):
928931
929932
930933def main ():
931- opt_parser = _make_opt_parser ()
932- opts , args = opt_parser .parse_args ()
934+ parser = _make_parser ()
935+ args = parser .parse_args ()
933936
934- if opts .processes < 0 :
935- opt_parser .error ("number of processes needs to be 0 or more" )
937+ if args .processes < 0 :
938+ parser .error ("number of processes needs to be 0 or more" )
936939
937- _configure_logging (opts )
940+ _configure_logging (args )
938941
939942 rc = 0
940- for bag_dir in args :
943+ for bag_dir in args . directory :
941944
942945 # validate the bag
943- if opts .validate :
946+ if args .validate :
944947 try :
945948 bag = Bag (bag_dir )
946949 # validate throws a BagError or BagValidationError
947- bag .validate (processes = opts .processes , fast = opts .fast )
948- if opts .fast :
950+ bag .validate (processes = args .processes , fast = args .fast )
951+ if args .fast :
949952 LOGGER .info ("%s valid according to Payload-Oxum" , bag_dir )
950953 else :
951954 LOGGER .info ("%s is valid" , bag_dir )
@@ -956,9 +959,9 @@ def main():
956959 # make the bag
957960 else :
958961 try :
959- make_bag (bag_dir , bag_info = opt_parser .bag_info ,
960- processes = opts .processes ,
961- checksum = opts .checksum )
962+ make_bag (bag_dir , bag_info = parser .bag_info ,
963+ processes = args .processes ,
964+ checksum = args .checksum )
962965 except Exception as exc :
963966 LOGGER .error ("Failed to create bag in %s: %s" , bag_dir , exc , exc_info = True )
964967 rc = 1
0 commit comments