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

Added help output when no arguments are given #99

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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