From 947638460091b7fa575d1a74d045b1acdf5b4acc Mon Sep 17 00:00:00 2001 From: John Gee Date: Tue, 20 Aug 2019 20:50:16 +1200 Subject: [PATCH] Allow options to come after commands --- CHANGELOG.md | 8 +++++++- bin/n | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 118d13f..063534d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. -## [6.0.1] (date goes here) +## [6.0.1] (2019-08-20) + +### Fixed + +- allow options to come after commands, especially `nvh lsr --all` ## [6.0.0] (2019-08-16) @@ -200,6 +204,8 @@ Only minor functional changes, but technically could break scripts relying on sp [Unreleased]: https://github.com/tj/n/compare/master...develop +[6.0.1]: https://github.com/tj/n/compare/v6.0.0...v6.0.1 +[6.0.0]: https://github.com/tj/n/compare/v5.0.2...v6.0.0 [5.0.2]: https://github.com/tj/n/compare/v5.0.1...v5.0.2 [5.0.1]: https://github.com/tj/n/compare/v5.0.0...v5.0.1 [5.0.0]: https://github.com/tj/n/compare/v4.1.0...v5.0.0 diff --git a/bin/n b/bin/n index 17028d0..777f614 100755 --- a/bin/n +++ b/bin/n @@ -40,7 +40,7 @@ function echo_red() { # Setup and state # -VERSION="6.0.1-0" +VERSION="6.0.1" N_PREFIX="${N_PREFIX-/usr/local}" N_PREFIX=${N_PREFIX%/} @@ -1144,22 +1144,39 @@ function show_diagnostics() { # Handle arguments. # +# First pass. Process the options so they can come before or after commands, +# particularly for `n lsr --all` and `n install --arch x686` +# which feel pretty natural. + +unprocessed_args=() + +while [[ $# -ne 0 ]]; do + case "$1" in + --all) N_MAX_REMOTE_MATCHES=32000 ;; + -V|--version) display_n_version ;; + -h|--help|help) display_help; exit ;; + -q|--quiet) set_quiet ;; + -d|--download) ACTIVATE=false ;; + --insecure) set_insecure ;; + --latest) display_remote_versions latest; exit ;; + --stable) display_remote_versions lts; exit ;; # [sic] old terminology + --lts) display_remote_versions lts; exit ;; + -a|--arch) shift; set_arch "$1";; # set arch and continue + exec) unprocessed_args=("$@"); break ;; + run|as|use) unprocessed_args=("$@"); break ;; + *) unprocessed_args+=( "$1" ) ;; + esac + shift +done + +set -- "${unprocessed_args[@]}" + if test $# -eq 0; then test -z "$(display_versions_paths)" && err_no_installed_print_help menu_select_cache_versions else while test $# -ne 0; do case "$1" in - --all) N_MAX_REMOTE_MATCHES=32000 ;; - -V|--version) display_n_version ;; - -h|--help|help) display_help; exit ;; - -q|--quiet) set_quiet ;; - -d|--download) ACTIVATE=false ;; - --insecure) set_insecure ;; - --latest) display_remote_versions latest; exit ;; - --stable) display_remote_versions lts; exit ;; # [sic] old terminology - --lts) display_remote_versions lts; exit ;; - -a|--arch) shift; set_arch "$1";; # set arch and continue bin|which) display_bin_path_for_version "$2"; exit ;; run|as|use) shift; run_with_version "$@"; exit ;; exec) shift; exec_with_version "$@"; exit ;;