Skip to content

Commit

Permalink
Merge pull request #99 from olehermanse/noargs
Browse files Browse the repository at this point in the history
Added help output when no arguments are given
  • Loading branch information
olehermanse authored Feb 22, 2022
2 parents 706c7de + 046d38a commit 095a39f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import logging as log

from cfbs.version import string as version
from cfbs.utils import user_error, is_cfbs_repo
from cfbs.utils import user_error, is_cfbs_repo, cache
from cfbs import commands


def get_args():
@cache
def _get_arg_parser():
command_list = [
cmd.split("_")[0] for cmd in dir(commands) if cmd.endswith("_command")
]
Expand Down Expand Up @@ -58,10 +59,12 @@ def get_args():
help="Keep order of items in the JSON in 'cfbs pretty'",
action="store_true",
)
return parser


def get_args():
parser = _get_arg_parser()
args = parser.parse_args()
if args.command == "help":
parser.print_help()
return args


Expand Down Expand Up @@ -94,7 +97,9 @@ def main() -> int:
return 0

if not args.command:
user_error("Usage: cfbs COMMAND")
_get_arg_parser().print_help()
print("")
user_error("No command given")

if args.non_interactive and args.command not in (
"init",
Expand All @@ -118,6 +123,7 @@ def main() -> int:

# Commands you can run outside a cfbs repo:
if args.command == "help":
_get_arg_parser().print_help()
return 0
if args.command == "init":
return commands.init_command(
Expand Down Expand Up @@ -157,4 +163,5 @@ def main() -> int:
if args.command == "update":
return commands.update_command(non_interactive=args.non_interactive)

_get_arg_parser().print_help()
user_error("Command '%s' not found" % args.command)
14 changes: 14 additions & 0 deletions cfbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,17 @@ def find(name, recursive=True, directories=False, files=True, extension=None):
yield os.path.join(root, file)
if not recursive:
return # End iteration after looking through first (top) level


def cache(func):
"""Memoization decorator similar to functools.cache (Python 3.9+)"""
memo = {}

def wrapper(*args, **kwargs):
kwargs = OrderedDict(sorted(kwargs.items()))
key = str({"args": args, "kwargs": kwargs})
if key not in memo:
memo[key] = func(*args, **kwargs)
return memo[key]

return wrapper

0 comments on commit 095a39f

Please sign in to comment.