Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option -m/--max #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add option -m/--max #67

wants to merge 3 commits into from

Conversation

donaldtn
Copy link

#65

    -m NUM, --max NUM
                          Truncate to NUM length.

Saw this request and thought it would be useful as long as some websites limit the length of passwords.

Copy link
Owner

@ulif ulif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @donaldtn for this PR. @madduck, are you comfortable with this change? It introduces a --max option that crops any generated phrase to a desired max length.

I think, I could live with it, although a "select enough phrases until it is short enough" might have been more elegant and kept the only-whole-words paradigm of diceware. On the other hand it could easily lead into endless loops (for instance, cause no phrase with only 1 char could be created)

@donaldtn: before merging I would like to address some minor things:

  1. The --max default should be None or -1 in my opinion, cause zero is a perfectly valid value here.
  2. The tests currently do not test much more, than two trivial usecases. It would be nice if there could be shown, how phrases are really cropped. I commented the tests directly.

While (1) must be addressed (or you can give me better arguments, why 0 should mean something different than other numbers), (2) is not that important. I could do that myself afterwards.

What do you think? Would that be okay for you? Don't worry about the nitpicking, your contribution is appreciated!

Comment on lines +77 to +80
options = handle_options(['-m', '0'])
assert options.max == 0
options = handle_options(['--max', '0'])
assert options.max == 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values apart from 0 would be more interesting here.

Comment on lines +256 to +260
def test_get_passphrase_max(self):
options = handle_options(args=[])
options.max = 15
phrase = get_passphrase(options)
assert len(phrase) == 15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would be more meaningful if it could ensure, that the original phrase was longer or shorter than 15 chars.

@@ -362,4 +377,4 @@ def test_main_wordlist(self, argv_handler, capsys, wordlists_dir):
sys.argv = ['diceware', '-w', 'foo']
main()
out, err = capsys.readouterr()
assert out == 'FooFooFooFooFooFoo\n'
assert out == 'FooFooFooFooFooFoo\n'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing newline is nitpicking, I know.

@@ -104,6 +104,9 @@ def handle_options(args):
parser.add_argument(
'-s', '--specials', default=0, type=int, metavar='NUM',
help="Insert NUM special chars into generated word.")
parser.add_argument(
'-m', '--max', default=0, type=int, metavar='NUM',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default -1 or None would be more appropriate, I think.

Comment on lines +199 to +200
if options.max > 0:
result = result[:options.max]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't truncating happen before inserting special chars (that might get lost this way round)?

@madduck
Copy link

madduck commented Oct 21, 2019

Thanks, this is indeed the most pragmatic solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants