-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from LachlanMarnham/basic-cli
Basic cli
- Loading branch information
Showing
14 changed files
with
402 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
from begin.registry import Registry # noqa: F401 | ||
|
||
|
||
__version__ = '0.3.0' | ||
VERSION = __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from argparse import ArgumentParser | ||
from dataclasses import dataclass | ||
from typing import ( | ||
Dict, | ||
List, | ||
) | ||
|
||
from begin.constants import DEFAULT_REGISTRY_NAME | ||
|
||
|
||
class Request: | ||
def __init__(self, target_identifier: str) -> None: | ||
target_name, _, registry_namespace = target_identifier.partition('@') | ||
self._target_name = target_name | ||
self._registry_namespace = registry_namespace or DEFAULT_REGISTRY_NAME | ||
self._options: Dict[str, str] = {} | ||
|
||
def add_option(self, param_identifier: str) -> None: | ||
key, _, value = param_identifier.partition(':') | ||
self._options[key] = value | ||
|
||
@property | ||
def target_name(self) -> str: | ||
return self._target_name | ||
|
||
@property | ||
def registry_namespace(self) -> str: | ||
return self._registry_namespace | ||
|
||
@property | ||
def options(self) -> Dict[str, str]: | ||
return self._options | ||
|
||
|
||
@dataclass | ||
class OptionalArg: | ||
short: str | ||
long: str | ||
default: str | ||
help: str | ||
|
||
|
||
OPTIONAL_ARGS = [ | ||
OptionalArg( | ||
short='-e', | ||
long='--extension', | ||
default='*targets.py', # TODO get this from settings | ||
help='The suffix to match target file patterns against.', | ||
), | ||
OptionalArg( | ||
short='-g', | ||
long='--global-dir', | ||
default='~/.begin', # TODO get this from settings | ||
help='The location of the directory holding global targets files.', | ||
), | ||
] | ||
|
||
|
||
@dataclass | ||
class ParsedCommand: | ||
extension: str | ||
global_dir: str | ||
requests: List[Request] | ||
|
||
|
||
def _parse_requests(args: List[str]) -> List[Request]: | ||
requests = [] | ||
request = None | ||
for arg in args: | ||
if ':' not in arg: | ||
# Not a key:value argument pair, must be either target or target@namespace | ||
if request is not None: | ||
requests.append(request) | ||
request = Request(arg) | ||
else: | ||
request.add_option(arg) | ||
requests.append(request) | ||
return requests | ||
|
||
|
||
def parse_command(): | ||
parser = ArgumentParser(description='A utility for running targets in a targets.py file.') | ||
|
||
for optional_arg in OPTIONAL_ARGS: | ||
parser.add_argument( | ||
optional_arg.short, | ||
optional_arg.long, | ||
default=optional_arg.default, | ||
help=optional_arg.help, | ||
) | ||
|
||
optional_args, request_args = parser.parse_known_args() | ||
requests = _parse_requests(request_args) | ||
return ParsedCommand( | ||
extension=optional_args.extension, | ||
global_dir=optional_args.global_dir, | ||
requests=requests, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Enable invocation of several chained recipes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Enable targets with parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add basic CLI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.