From 9c725ffc7d155a63c0f3a8fc192c5696a8b226d6 Mon Sep 17 00:00:00 2001 From: Gareth Latty Date: Wed, 15 Nov 2017 16:54:50 +0000 Subject: [PATCH] Resolve #8: Replace optparse with argparse. --- README | 18 ------------- README.md | 51 ++++++++++++++++++++++++++++++++++++ unrpa | 78 +++++++++++++++++++++++++++---------------------------- 3 files changed, 90 insertions(+), 57 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index 566f19e..0000000 --- a/README +++ /dev/null @@ -1,18 +0,0 @@ -Unrpa is a script to extract files from archives created for the Ren'Py Visual Novel Engine (http://www.renpy.org/). - -You will need Python 3.x in order to run it (either install through your package manager or see -https://www.python.org/downloads/). - -Usage: unrpa [options] pathname - -Options: - --version show program's version number and exit - -h, --help show this help message and exit - -v, --verbose explain what is being done [default]. - -s, --silent no output. - -l, --list only list contents, do not extract. - -p PATH, --path=PATH will extract to the given path. - -m, --mkdir will make any non-existent directories in extraction - path. - -f VERSION, --force=VERSION forces an archive version. May result in failure. - --continue-on-error try to continue extraction when something goes wrong. diff --git a/README.md b/README.md new file mode 100644 index 0000000..eeda129 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# unrpa - Extract files from the RPA archive format. + +## About + +unrpa is a script to extract files from the RPA archive format created +for [the Ren'Py Visual Novel Engine](http://www.renpy.org/). + +## Dependencies + +You will need Python 3.x in order to run it (either install through +your package manager or +[directly from python.org](https://www.python.org/downloads/)). + +## Installation + +You can [download the latest release](https://github.com/Lattyware/unrpa/releases/latest) +and then run the script as described below. + +## Command Line Usage + +``` +usage: unrpa [-h] [-v] [-s] [-l] [-p PATH] [-m] [-f VERSION] + [--continue-on-error] + FILENAME +``` + +### Options + +| Positional Argument | Description | +|---------------------|--------------------------| +| FILENAME | the RPA file to extract. | + +| Optional Argument | Description | +|------------------------------|------------------------------------------------------------| +| -h, --help | show this help message and exit | +| -v, --verbose | explain what is being done [default]. | +| -s, --silent | no output. | +| -l, --list | only list contents, do not extract. | +| -p PATH, --path PATH | will extract to the given path. | +| -m, --mkdir | will make any non-existent directories in extraction path. | +| -f VERSION, --force VERSION | forces an archive version. May result in failure. | +| --continue-on-error | try to continue extraction when something goes wrong. | + +### Examples + + - On most unix systems, open a terminal, then: + `python3 unrpa -mp "path/to/output/dir" "path/to/archive.rpa"` + - On most Windows systems, open a Command Prompt, then: + `py -3 unrpa -mp "path\to\output\dir" "path\to\archive.rpa"` + + diff --git a/unrpa b/unrpa index dd6f8a3..bcb4935 100755 --- a/unrpa +++ b/unrpa @@ -18,7 +18,7 @@ along with this program. If not, see . """ import os -import optparse +import argparse import sys import pickle import zlib @@ -144,44 +144,44 @@ class UnRPA: if __name__ == "__main__": - parser = optparse.OptionParser(usage="usage: %prog [options] pathname", version="%prog 1.1") - - parser.add_option("-v", "--verbose", action="count", dest="verbose", - help="explain what is being done [default].") - parser.add_option("-s", "--silent", action="store_const", const=0, dest="verbose", default=1, - help="no output.") - parser.add_option("-l", "--list", action="store_true", dest="list", default=False, - help="only list contents, do not extract.") - parser.add_option("-p", "--path", action="store", type="string", dest="path", default=None, - help="will extract to the given path.") - parser.add_option("-m", "--mkdir", action="store_true", dest="mkdir", default=False, - help="will make any non-existent directories in extraction path.") - parser.add_option("-f", "--force", action="store", type="int", dest="version", default=None, - help="forces an archive version. May result in failure.") - parser.add_option("--continue-on-error", action="store_true", dest="continue_on_error", default=False, - help="try to continue extraction when something goes wrong.") - - (options, args) = parser.parse_args() - - if not len(args) == 1: - if options.verbose: - parser.print_help() - parser.error("incorrect number of arguments.") - - if options.list and options.path: - parser.error("option -p: only valid when extracting.") - - if options.mkdir and not options.path: - parser.error("option -m: only valid when --path (-p) is set.") - - if options.list and options.verbose == 0: - parser.error("option -l: can't be silent while listing data.") - - filename = args[0] - - extractor = UnRPA(filename, options.verbose, options.path, options.mkdir, options.version, - options.continue_on_error) - if options.list: + parser = argparse.ArgumentParser(description="Extract files from the RPA archive format.") + + parser.add_argument("-v", "--verbose", action="count", dest="verbose", default=1, + help="explain what is being done [default].") + parser.add_argument("-s", "--silent", action="store_const", const=0, dest="verbose", + help="no output.") + parser.add_argument("-l", "--list", action="store_true", dest="list", default=False, + help="only list contents, do not extract.") + parser.add_argument("-p", "--path", action="store", type=str, dest="path", default=None, + help="will extract to the given path.") + parser.add_argument("-m", "--mkdir", action="store_true", dest="mkdir", default=False, + help="will make any non-existent directories in extraction path.") + parser.add_argument("-f", "--force", action="store", type=int, dest="version", default=None, + help="forces an archive version. May result in failure.") + parser.add_argument("--continue-on-error", action="store_true", dest="continue_on_error", default=False, + help="try to continue extraction when something goes wrong.") + + parser.add_argument("filename", metavar="FILENAME", type=str, help="the RPA file to extract.") + + args = parser.parse_args() + + if args.list and args.path: + parser.error("option -path: only valid when extracting.") + + if args.mkdir and not args.path: + parser.error("option --mkdir: only valid when --path is set.") + + if not args.mkdir and args.path and not os.path.isdir(args.path): + parser.error("No such directory: '{}'. Use --mkdir to create it.".format(args.path)) + + if args.list and args.verbose == 0: + parser.error("option --list: can't be silent while listing data.") + + if not os.path.isfile(args.filename): + parser.error("No such file: '{}'.".format(args.filename)) + + extractor = UnRPA(args.filename, args.verbose, args.path, args.mkdir, args.version, args.continue_on_error) + if args.list: extractor.list_files() else: extractor.extract_files()