diff -r 1bf721567067 Lib/tarfile.py --- a/Lib/tarfile.py Fri Mar 01 21:28:23 2013 +0200 +++ b/Lib/tarfile.py Sun Mar 03 09:50:42 2013 +0530 @@ -27,6 +27,18 @@ # OTHER DEALINGS IN THE SOFTWARE. # """Read from and write to tar format archives. + +Command line usage: + +Options: + -o/--output -e/--extract ... : Extract the + files , ,... mentioned as the arguments from + with output folder as dir. + (If not specified extracts in current dir.) + (If ... not specified extracts all the files.) + -c/--create .. ... : Creates an archive of + files , , ... with the name of the archive as . + -l/--list : Lists the members of the archive """ version = "0.9.0" @@ -2426,3 +2438,58 @@ bltn_open = open open = TarFile.open + + +def main(args=None): + if args is None: + args = sys.argv[1:] + output = "" + import getopt + try: + opts, args = getopt.getopt(args, "e:l:c:o:h", + ["extract=", "list=", "create=", "output=", + "help"]) + except getopt.error as err: + print(err) + print("user -h/--help for command line help") + return 2 + for o, file in opts: + if o in("-e", "--extract"): + t = open(file, 'r') + if args==[]: + t.extractall(output) + else: + for member in args: + t.extract(member, output) + if o in("-l", "--list"): + t = open(file, 'r') + t.list() + if o in("-c", "--create"): + try: + mode = file.rpartition('.')[2] + except AttributeError: + print("Output file name not correct") + if mode in ('gz', 'bz2', 'xz'): + mode = 'w:' + mode + elif mode == 'tgz': + mode = 'gz' + elif mode in ('tbz', 'tbz2', 'tb2'): + mode = 'bz2' + elif mode == 'txz': + mode = 'xz' + else: + mode = 'w' + t = open(file, mode) + for member in args: + t.add(member) + if o in ("-o", "--output"): + output = file + if o in("-h", "--help"): + print(__doc__, end=" ") + return 0 + t.close() + return None + + +if __name__ == "__main__": + sys.exit(main())