From e20181f092d9a69b3c38905e975d4c3aad190fb0 Mon Sep 17 00:00:00 2001 From: ulif <uli@gnufix.de> Date: Wed, 28 Jan 2015 10:19:04 +0100 Subject: [PATCH] Add a argparser. --- diceware/diceware.py | 9 +++++++++ tests/test_diceware.py | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/diceware/diceware.py b/diceware/diceware.py index c994f57..3840846 100644 --- a/diceware/diceware.py +++ b/diceware/diceware.py @@ -1,3 +1,4 @@ +import argparse import os import re from random import SystemRandom @@ -13,6 +14,14 @@ RE_ASCII_CHARS = re.compile('^[a-zA-Z]{2}$') SPECIAL_CHARS = "~!#$%^&*()-=+[]\{}:;\"'<>?/0123456789" +def handle_options(args): + """Handle commandline options. + """ + parser = argparse.ArgumentParser(description="Create a passphrase") + args = parser.parse_args(args) + return args + + def get_wordlist(path): """Parse file at `path` and build a word list of it. diff --git a/tests/test_diceware.py b/tests/test_diceware.py index 00801af..2bcea2c 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -1,7 +1,8 @@ import os import pytest +import sys from diceware.diceware import ( - SRC_DIR, get_wordlist, get_wordlist_path, get_passphrase, + SRC_DIR, get_wordlist, get_wordlist_path, get_passphrase, handle_options, ) @@ -53,3 +54,20 @@ class TestDicewareModule(object): r1 = get_passphrase() r2 = get_passphrase() assert r1 != r2 + + def test_handle_options(self, capsys): + # we can get help + with pytest.raises(SystemExit) as exc_info: + handle_options(['--help']) + assert exc_info.value.code == 0 + out, err = capsys.readouterr() + out = out.replace( + os.path.basename(sys.argv[0]), 'diceware') + assert out == ( + 'usage: diceware [-h]\n' + '\n' + 'Create a passphrase\n' + '\n' + 'optional arguments:\n' + ' -h, --help show this help message and exit\n' + ) -- GitLab