diff --git a/diceware/__init__.py b/diceware/__init__.py index e325774402294a3ab2908c0f9e807db968e91489..01cd5660c0baccbbe2ed2d1c9202f96f0d79b463 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -86,6 +86,7 @@ def get_random_sources(): def handle_options(args): """Handle commandline options. """ + random_sources = get_random_sources().keys() parser = argparse.ArgumentParser(description="Create a passphrase") parser.add_argument( '-n', '--num', default=6, type=int, @@ -103,6 +104,12 @@ def handle_options(args): parser.add_argument( '-d', '--delimiter', default='', help="Separate words by DELIMITER. Empty string by default.") + parser.add_argument( + '-r', '--randomsource', default='system', choices=random_sources, + metavar="SOURCE", + help=( + "Get randomness from this source. Possible values: `%s'. " + "Default: system" % "', `".join(random_sources))) parser.add_argument( 'infile', nargs='?', metavar='INFILE', default=None, type=argparse.FileType('r'), diff --git a/tests/exp_help_output.txt b/tests/exp_help_output.txt index 0358e4712494e3084a344a29272c62d1cac710de..53fc85bdc10ab4fee81776727965536bd0a9af82 100644 --- a/tests/exp_help_output.txt +++ b/tests/exp_help_output.txt @@ -1,5 +1,5 @@ usage: diceware [-h] [-n NUM] [-c | --no-caps] [-s NUM] [-d DELIMITER] - [--version] + [-r SOURCE] [--version] [INFILE] Create a passphrase @@ -16,4 +16,7 @@ optional arguments: Insert NUM special chars into generated word. -d DELIMITER, --delimiter DELIMITER Separate words by DELIMITER. Empty string by default. + -r SOURCE, --randomsource SOURCE + Get randomness from this source. Possible values: + `system'. Default: system --version output version information and exit. diff --git a/tests/test_diceware.py b/tests/test_diceware.py index fa4768eacb6d8444d575a9a817f205df0fddc985..71b2bc4e6d9602ab33fc9ea789b8782759fceb46 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -211,6 +211,7 @@ class TestDicewareModule(object): assert options.infile is None assert options.version is False assert options.delimiter == "" + assert options.randomsource == "system" def test_handle_options_infile(self, tmpdir): # we can give an infile @@ -235,6 +236,23 @@ class TestDicewareModule(object): options = handle_options(['-d', 'WOW']) assert options.delimiter == 'WOW' + def test_handle_options_randomsource(self): + # we can choose the source of randomness + source_names = get_random_sources().keys() + for name in source_names: + options = handle_options(['-r', name]) + assert options.randomsource == name + options = handle_options(['--randomsource', name]) + assert options.randomsource == name + + def test_handle_options_randomsource_rejects_invalid(self, capsys): + # we can not choose illegal values for source of randomness + with pytest.raises(SystemExit): + handle_options(['-r', 'not-a-valid-source-name']) + out, err = capsys.readouterr() + assert out == '' + assert "invalid choice" in err + def test_main(self, capsys): # we can get a passphrase main([]) # call with default options in place