diff -r 1bf721567067 Lib/tarfile.py --- a/Lib/tarfile.py Fri Mar 01 21:28:23 2013 +0200 +++ b/Lib/tarfile.py Thu Mar 07 18:40:13 2013 +0530 @@ -27,6 +27,20 @@ # OTHER DEALINGS IN THE SOFTWARE. # """Read from and write to tar format archives. + +Command line usage: + +Options: + -o/--output -e/--extract ... : Extract the + files , ,... 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 files of the archive + --gz/--gunzip/--gzip/--tgz/-z/--ungzip/--bz2/--bzip2/--tbz2/--tbz/ + --tb2/--xz/--lzma: Compression to be used. + -d/--doc: To print __doc__ """ version = "0.9.0" @@ -2426,3 +2440,67 @@ bltn_open = open open = TarFile.open + + +def main(): + import argparse + + output = os.curdir + parser = argparse.ArgumentParser("tarfile Command line") + parser.add_argument('-l', '--list', metavar = '', + help = 'list all the files in the archive') + parser.add_argument('-e', '--extract', nargs = '*', + metavar = ('', ''), + help = 'extract files, if files not specified \ + extract all. If -o/-outdir specified extracts \ + in that dir') + parser.add_argument('-c', '--create', nargs = '*', + metavar = ('', ''), + help = 'create an archive of the files \ + specified. Compression should be specified') + parser.add_argument('-o', '--outdir', metavar = '', + help = 'output dir of the extracted files') + parser.add_argument('--gz', '--gunzip', '--gzip', '--tgz', '-z', + '--ungzip', action = 'store_true', + help = 'gz compression') + parser.add_argument('--bz2', '--bzip2', '--tbz2', '--tbz', '--tb2', + action = 'store_true', help = 'bz2 compression') + parser.add_argument('--xz', '--lzma', action = 'store_true', + help = 'xz compression') + parser.add_argument('-d', '--doc', action = 'store_true', + help = 'documentation') + args = parser.parse_args() + + if args.list: + with open(args.list) as t: + t.list() + + if args.outdir: + output = args.outdir + + if args.extract: + with open(args.extract.pop(0)) as t: + if not args.extract: + t.extractall(output) + else: + for member in args.extract: + t.extract(member, output) + + if args.create: + if args.gz: + mode = 'w:gz' + elif args.bz2: + mode = 'w:bz2' + elif args.xz: + mode = 'w:xz' + else: + mode = 'w' + with open(args.create.pop(0), mode) as t: + for file in args.create: + t.add(file) + + if args.doc: + sys.stdout.write(__doc__) + +if __name__ == "__main__": + sys.exit(main())