Skip to content

Commit

Permalink
Resolve #8: Replace optparse with argparse.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lattyware committed Nov 15, 2017
1 parent fee87ef commit 9c725ff
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 57 deletions.
18 changes: 0 additions & 18 deletions README

This file was deleted.

51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"`


78 changes: 39 additions & 39 deletions unrpa
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import os
import optparse
import argparse
import sys
import pickle
import zlib
Expand Down Expand Up @@ -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()

0 comments on commit 9c725ff

Please sign in to comment.