From ab4ffcc51d8fe1407cd51dc75d157e556f8a2adf Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 15 Nov 2018 22:08:16 -0700 Subject: [PATCH] Be more helpful to newcomers. The error output when the bootstrapper is called without arguments or with -h is lacking. Try to improve it. Tweak the output when called without arguments when an installation is found as well, in the same spirit. Finally, change the print_usage() to a print_help() when no command is given, to get additional context. Fixes: #108 Signed-off-by: Marti Bolivar --- src/west/_bootstrap/main.py | 44 +++++++++++++++++++++++++++---------- src/west/main.py | 8 ++++--- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/west/_bootstrap/main.py b/src/west/_bootstrap/main.py index 9da535d..5770a39 100644 --- a/src/west/_bootstrap/main.py +++ b/src/west/_bootstrap/main.py @@ -123,7 +123,8 @@ def init(argv): formatter_class=argparse.RawDescriptionHelpFormatter, description= ''' -Initializes a Zephyr installation. +Initializes a Zephyr installation. Use "west clone" afterwards to fetch the +sources. In more detail, does the following: @@ -182,7 +183,7 @@ to handle any resetting yourself. installation to update the manifest or west URL/revision''') init_parser.add_argument( - 'directory', nargs='?', + 'directory', nargs='?', default=None, help='''Directory to initialize West in. Missing directories will be created automatically. (default: current directory)''') @@ -247,7 +248,8 @@ def bootstrap(args): with open(os.path.join(directory, WEST_DIR, WEST_MARKER), 'w') as f: hide_file(f.name) - print('=== West initialized ===') + print('=== West initialized. Now run "west clone" in {}. ==='. + format(directory)) def reinit(config_path, args): @@ -355,21 +357,41 @@ def append_to_pythonpath(directory): def wrap(argv): printing_version = False + printing_help_only = False - if argv and argv[0] in ('-V', '--version'): - print('West bootstrapper version: v{} ({})'.format(version.__version__, - os.path.dirname(__file__))) - printing_version = True + if argv: + if argv[0] in ('-V', '--version'): + print('West bootstrapper version: v{} ({})'. + format(version.__version__, os.path.dirname(__file__))) + printing_version = True + elif len(argv) == 1 and argv[0] in ('-h', '--help'): + # This only matters if we're called outside of an + # installation directory. We delegate to the main help if + # called from within one, because it includes a list of + # available commands, etc. + printing_help_only = True + start = os.getcwd() try: - topdir = west_topdir() + topdir = west_topdir(start) except WestNotFound: if printing_version: sys.exit(0) # run outside of an installation directory + elif printing_help_only: + # We call print multiple times here and below instead of using + # \n to be newline agnostic. + print('To set up a Zephyr installation here, run "west init".') + print('Run "west init -h" for additional information.') + sys.exit(0) else: - sys.exit('Error: not a Zephyr directory (or any parent): {}\n' - 'Use "west init" to install Zephyr here' - .format(os.getcwd())) + print('Error: "{}" is not a Zephyr installation directory.'. + format(start), file=sys.stderr) + print('Things to try:', file=sys.stderr) + print(' - Run "west init" to set up an installation here.', + file=sys.stderr) + print(' - Run "west init -h" for additional information.', + file=sys.stderr) + sys.exit(1) west_git_repo = os.path.join(topdir, WEST_DIR, WEST) if printing_version: diff --git a/src/west/main.py b/src/west/main.py index a2a6668..2f889de 100755 --- a/src/west/main.py +++ b/src/west/main.py @@ -25,7 +25,7 @@ from west.commands.project import List, Clone, Fetch, Pull, Rebase, Branch, \ Checkout, Diff, Status, Update, ForAll, \ WestUpdated from west.manifest import Manifest -from west.util import quote_sh_list, in_multirepo_install +from west.util import quote_sh_list, in_multirepo_install, west_dir IN_MULTIREPO_INSTALL = in_multirepo_install(__file__) @@ -191,8 +191,10 @@ def parse_args(argv): set_zephyr_base(args) if 'handler' not in args: - log.err('you must specify a command', fatal=True) - west_parser.print_usage(file=sys.stderr) + log.err('west installation found (in {}), but no command given'. + format(west_dir()), + fatal=True) + west_parser.print_help(file=sys.stderr) sys.exit(1) return args, unknown