Skip to content

Commit

Permalink
Rewrite _check_args using rules for required params. Consolidates log…
Browse files Browse the repository at this point in the history
…ic and reduces cyclomatic complexity to 2.
  • Loading branch information
jaraco committed Apr 26, 2024
1 parent f922be6 commit 33bae95
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions keyring/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ def run(self, argv):
return method()

def _check_args(self):
if self.operation:
if self.operation == 'get' and self.get_mode == 'creds':
if self.service is None:
self.parser.error(f"{self.operation} requires service")
elif self.service is None or self.username is None:
self.parser.error(f"{self.operation} requires service and username")
needs_username = self.operation != 'get' or self.get_mode != 'creds'
required = (['service'] + ['username'] * needs_username) * bool(self.operation)
if any(getattr(self, param) is None for param in required):
self.parser.error(f"{self.operation} requires {' and '.join(required)}")

def do_get(self):
credential = getattr(self, f'_get_{self.get_mode}')()
Expand Down

0 comments on commit 33bae95

Please sign in to comment.