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

Allow combining short CLI arguments #32

Merged
merged 10 commits into from
Dec 2, 2021
Merged

Allow combining short CLI arguments #32

merged 10 commits into from
Dec 2, 2021

Conversation

rocketdey
Copy link
Collaborator

@rocketdey rocketdey commented Nov 30, 2021

Instead of -A -r recipient1 -r recipient2 you can now write -Arr recipient1 recipient2.

Fixes #25

@covert-encryption
Copy link
Owner

Thanks for the PR. Looks mostly good and the change is certainly welcome, but I will add some comments.

Copy link
Owner

@covert-encryption covert-encryption left a comment

Choose a reason for hiding this comment

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

Just some comments mostly on coding style so far. The algorithm itself needs a bit of work to avoid the problem seen with the test. Perhaps you could have another look at that?

tests/test_cli.py Outdated Show resolved Hide resolved
tests/test_cli.py Outdated Show resolved Hide resolved
covert/__main__.py Outdated Show resolved Hide resolved
covert/__main__.py Outdated Show resolved Hide resolved
@covert-encryption

This comment has been minimized.

@covert-encryption
Copy link
Owner

Please commit this test_cli.py:

import pytest
import sys

def test_argparser(capsys):
  from covert.__main__ import argparse  # Import *after* capsys wraps stdout/stderr
  
  # Correct but complex arguments
  sys.argv = "covert enc --recipient asdf1 -r asdf2 -Arrp recipient1 recipient2".split()
  a = argparse()
  assert a.recipients == ['asdf1', 'asdf2', 'recipient1', 'recipient2']
  assert a.paste is True
  assert a.askpass == 1
  # Should produce no output
  cap = capsys.readouterr()
  assert not cap.out
  assert not cap.err

  # Missing argument parameter
  sys.argv = "covert enc -Arrp recipient1".split()
  with pytest.raises(SystemExit):
    argparse()
  cap = capsys.readouterr()
  assert not cap.out
  assert "Argument parameter missing: covert enc -Arrp …" in cap.err

The tests are passing and the implementation seems to be working. Very close to being complete then, just minor cleanup needed.

@covert-encryption covert-encryption changed the title Fix for the Issue #25 Allow combining short CLI arguments Dec 1, 2021
__main__.py Outdated
Comment on lines 145 to 153
if isinstance(a, str) is True:
a = [a]
for i, av in enumerate(a):
argvar = next((k for k, v in ad.items() if av in v), None)
if isinstance(av, int) is True:
continue
if av is None:
stderr.write(f'{modehelp}\n 💣 Unknown argument: covert {args.mode} {aprint}\n')
sys.exit(1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed == to is or is not.

Copy link
Owner

Choose a reason for hiding this comment

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

Do not use is True unless when you have to (e.g. to tell 1 and True apart).

__main__.py Outdated
if any(longarg[1:] in a for longarg in longargs) is True:
stderr.write(f' 💣 Multiple arguments cannot have long arguments: covert {args.mode} {a}\n')
sys.exit(1)
elif any(arg not in shortargs for arg in list(a[1:])) is True:
Copy link
Owner

Choose a reason for hiding this comment

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

Remove the is True, it is not needed.

__main__.py Outdated
else:
setattr(args, argvar, True)
print(var)
if isinstance(var, int) is not True and var is not None:
Copy link
Owner

Choose a reason for hiding this comment

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

if not isinstance(var, int) and var is not None:

Copy link
Owner

Choose a reason for hiding this comment

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

Looks like what you really wanted here is isinstance(var, list) or isinstance(var, (str, list)) but as commented below, this entire check should be removed.

__main__.py Outdated
Comment on lines 135 to 137
if any(longarg[1:] in a for longarg in longargs) is True:
stderr.write(f' 💣 Multiple arguments cannot have long arguments: covert {args.mode} {a}\n')
sys.exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

Should not test for this. -out should be allowed and it means -o -u -t, not --out.

__main__.py Outdated
Comment on lines 166 to 169
if var[-1].startswith('-') or var[-1].startswith('--'):
if var[-1][1:] in longargs or var[-1][1:] in shortargs:
stderr.write(f'{modehelp}\n 💣 Argument parameter cannot be the same with arguments: covert {args.mode} {aprint} {var[-1]} …\n')
sys.exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

Argument parameter CAN be the same as any argument. Remove this check.

@covert-encryption covert-encryption merged commit 7c417f2 into covert-encryption:main Dec 2, 2021
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.

Allow combining short arguments in CLI argument parser
2 participants