From 2ea77f3883bec2aa777c0216bc9e840bad3bcca5 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 15 Jul 2017 13:24:31 +0900 Subject: [PATCH] Generate completions for all shells --- Cargo.toml | 4 + build.rs | 15 ++++ completions/_gutenberg | 101 +++++++++++++++++++++ completions/_gutenberg.ps1 | 74 ++++++++++++++++ completions/gutenberg.bash-completion | 121 ++++++++++++++++++++++++++ completions/gutenberg.fish | 28 ++++++ src/cli.rs | 24 +++++ src/main.rs | 21 +---- 8 files changed, 369 insertions(+), 19 deletions(-) create mode 100644 build.rs create mode 100644 completions/_gutenberg create mode 100644 completions/_gutenberg.ps1 create mode 100644 completions/gutenberg.bash-completion create mode 100644 completions/gutenberg.fish create mode 100644 src/cli.rs diff --git a/Cargo.toml b/Cargo.toml index cf93112..3c49a78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,10 @@ description = "Static site generator" homepage = "https://github.com/Keats/gutenberg" repository = "https://github.com/Keats/gutenberg" keywords = ["static", "site", "generator", "blog"] +build = "build.rs" + +[build-dependencies] +clap = "2" [[bin]] name = "gutenberg" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..67907d9 --- /dev/null +++ b/build.rs @@ -0,0 +1,15 @@ +#[macro_use] +extern crate clap; + +use clap::Shell; + +include!("src/cli.rs"); + +fn main() { + let mut app = build_cli(); + println!("hello"); + app.gen_completions("gutenberg", Shell::Bash, "completions/"); + app.gen_completions("gutenberg", Shell::Fish, "completions/"); + app.gen_completions("gutenberg", Shell::Zsh, "completions/"); + app.gen_completions("gutenberg", Shell::PowerShell, "completions/"); +} diff --git a/completions/_gutenberg b/completions/_gutenberg new file mode 100644 index 0000000..68f660d --- /dev/null +++ b/completions/_gutenberg @@ -0,0 +1,101 @@ +#compdef gutenberg + +_gutenberg() { + typeset -A opt_args + local ret=1 + + local context curcontext="$curcontext" state line + _arguments -s -S -C \ +"-c+[Path to a config file other than config.toml]" \ +"--config+[Path to a config file other than config.toml]" \ +"-h[Prints help information]" \ +"--help[Prints help information]" \ +"-V[Prints version information]" \ +"--version[Prints version information]" \ +"1:: :_gutenberg_commands" \ +"*:: :->Gutenberg" \ +&& ret=0 + case $state in + (Gutenberg) + curcontext="${curcontext%:*:*}:gutenberg-command-$words[1]:" + case $line[1] in + (init) +_arguments -s -S -C \ +"-h[Prints help information]" \ +"--help[Prints help information]" \ +"-V[Prints version information]" \ +"--version[Prints version information]" \ +"1:: :_gutenberg__init_commands" \ +&& ret=0 +;; +(build) +_arguments -s -S -C \ +"-h[Prints help information]" \ +"--help[Prints help information]" \ +"-V[Prints version information]" \ +"--version[Prints version information]" \ +&& ret=0 +;; +(serve) +_arguments -s -S -C \ +"-h[Prints help information]" \ +"--help[Prints help information]" \ +"-V[Prints version information]" \ +"--version[Prints version information]" \ +"1:: :_gutenberg__serve_commands" \ +&& ret=0 +;; +(help) +_arguments -s -S -C \ +"-h[Prints help information]" \ +"--help[Prints help information]" \ +"-V[Prints version information]" \ +"--version[Prints version information]" \ +&& ret=0 +;; + esac + ;; +esac +} + +(( $+functions[_gutenberg_commands] )) || +_gutenberg_commands() { + local commands; commands=( + "init:Create a new Gutenberg project" \ +"build:Builds the site" \ +"serve:Serve the site. Rebuild and reload on change automatically" \ +"help:Prints this message or the help of the given subcommand(s)" \ + ) + _describe -t commands 'gutenberg commands' commands "$@" +} +(( $+functions[_gutenberg__build_commands] )) || +_gutenberg__build_commands() { + local commands; commands=( + + ) + _describe -t commands 'gutenberg build commands' commands "$@" +} +(( $+functions[_gutenberg__help_commands] )) || +_gutenberg__help_commands() { + local commands; commands=( + + ) + _describe -t commands 'gutenberg help commands' commands "$@" +} +(( $+functions[_gutenberg__init_commands] )) || +_gutenberg__init_commands() { + local commands; commands=( + "NAME:Name of the project. Will create a directory with that name in the current directory" \ + ) + _describe -t commands 'gutenberg init commands' commands "$@" +} +(( $+functions[_gutenberg__serve_commands] )) || +_gutenberg__serve_commands() { + local commands; commands=( + "INTERFACE:Interface to bind on (default to 127.0.0.1)" \ +"PORT:Which port to use (default to 1111)" \ + ) + _describe -t commands 'gutenberg serve commands' commands "$@" +} + +_gutenberg "$@" \ No newline at end of file diff --git a/completions/_gutenberg.ps1 b/completions/_gutenberg.ps1 new file mode 100644 index 0000000..ec383f1 --- /dev/null +++ b/completions/_gutenberg.ps1 @@ -0,0 +1,74 @@ + +@('gutenberg', './gutenberg') | %{ + Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $command = '_gutenberg' + $commandAst.CommandElements | + Select-Object -Skip 1 | + %{ + switch ($_.ToString()) { + + 'Gutenberg' { + $command += '_Gutenberg' + break + } + + 'init' { + $command += '_init' + break + } + + 'build' { + $command += '_build' + break + } + + 'serve' { + $command += '_serve' + break + } + + 'help' { + $command += '_help' + break + } + + default { + break + } + } + } + + $completions = @() + + switch ($command) { + + '_gutenberg' { + $completions = @('init', 'build', 'serve', 'help', '-h', '-V', '-c', '--help', '--version', '--config') + } + + '_gutenberg_init' { + $completions = @('-h', '-V', '--help', '--version') + } + + '_gutenberg_build' { + $completions = @('-h', '-V', '--help', '--version') + } + + '_gutenberg_serve' { + $completions = @('-h', '-V', '--help', '--version') + } + + '_gutenberg_help' { + $completions = @('-h', '-V', '--help', '--version') + } + + } + + $completions | + ?{ $_ -like "$wordToComplete*" } | + Sort-Object | + %{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ } + } +} diff --git a/completions/gutenberg.bash-completion b/completions/gutenberg.bash-completion new file mode 100644 index 0000000..9b48b69 --- /dev/null +++ b/completions/gutenberg.bash-completion @@ -0,0 +1,121 @@ +_gutenberg() { + local i cur prev opts cmds + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + cmd="" + opts="" + + for i in ${COMP_WORDS[@]} + do + case "${i}" in + gutenberg) + cmd="gutenberg" + ;; + + build) + cmd+="__build" + ;; + help) + cmd+="__help" + ;; + init) + cmd+="__init" + ;; + serve) + cmd+="__serve" + ;; + *) + ;; + esac + done + + case "${cmd}" in + gutenberg) + opts=" -h -V -c --help --version --config init build serve help" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + case "${prev}" in + + --config) + COMPREPLY=("") + return 0 + ;; + -c) + COMPREPLY=("") + return 0 + ;; + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + + gutenberg__build) + opts=" -h -V --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + gutenberg__help) + opts=" -h -V --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + gutenberg__init) + opts=" -h -V --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + gutenberg__serve) + opts=" -h -V --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + esac +} + +complete -F _gutenberg -o bashdefault -o default gutenberg diff --git a/completions/gutenberg.fish b/completions/gutenberg.fish new file mode 100644 index 0000000..7e5d6fa --- /dev/null +++ b/completions/gutenberg.fish @@ -0,0 +1,28 @@ +function __fish_using_command + set cmd (commandline -opc) + if [ (count $cmd) -eq (count $argv) ] + for i in (seq (count $argv)) + if [ $cmd[$i] != $argv[$i] ] + return 1 + end + end + return 0 + end + return 1 +end + +complete -c gutenberg -n "__fish_using_command gutenberg" -s c -l config -d "Path to a config file other than config.toml" +complete -c gutenberg -n "__fish_using_command gutenberg" -s h -l help -d "Prints help information" +complete -c gutenberg -n "__fish_using_command gutenberg" -s V -l version -d "Prints version information" +complete -c gutenberg -n "__fish_using_command gutenberg" -f -a "init" -d "Create a new Gutenberg project" +complete -c gutenberg -n "__fish_using_command gutenberg" -f -a "build" -d "Builds the site" +complete -c gutenberg -n "__fish_using_command gutenberg" -f -a "serve" -d "Serve the site. Rebuild and reload on change automatically" +complete -c gutenberg -n "__fish_using_command gutenberg" -f -a "help" -d "Prints this message or the help of the given subcommand(s)" +complete -c gutenberg -n "__fish_using_command gutenberg init" -s h -l help -d "Prints help information" +complete -c gutenberg -n "__fish_using_command gutenberg init" -s V -l version -d "Prints version information" +complete -c gutenberg -n "__fish_using_command gutenberg build" -s h -l help -d "Prints help information" +complete -c gutenberg -n "__fish_using_command gutenberg build" -s V -l version -d "Prints version information" +complete -c gutenberg -n "__fish_using_command gutenberg serve" -s h -l help -d "Prints help information" +complete -c gutenberg -n "__fish_using_command gutenberg serve" -s V -l version -d "Prints version information" +complete -c gutenberg -n "__fish_using_command gutenberg help" -s h -l help -d "Prints help information" +complete -c gutenberg -n "__fish_using_command gutenberg help" -s V -l version -d "Prints version information" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..a25fcde --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,24 @@ +use clap::App; + + +pub fn build_cli() -> App<'static, 'static> { + clap_app!(Gutenberg => + (version: crate_version!()) + (author: "Vincent Prouillet") + (about: "Static site generator") + (@setting SubcommandRequiredElseHelp) + (@arg config: -c --config +takes_value "Path to a config file other than config.toml") + (@subcommand init => + (about: "Create a new Gutenberg project") + (@arg name: +required "Name of the project. Will create a directory with that name in the current directory") + ) + (@subcommand build => + (about: "Builds the site") + ) + (@subcommand serve => + (about: "Serve the site. Rebuild and reload on change automatically") + (@arg interface: "Interface to bind on (default to 127.0.0.1)") + (@arg port: "Which port to use (default to 1111)") + ) + ) +} diff --git a/src/main.rs b/src/main.rs index 96ffafb..29cb6f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,28 +20,11 @@ use std::time::Instant; mod cmd; mod console; mod rebuild; +mod cli; fn main() { - let matches = clap_app!(Gutenberg => - (version: crate_version!()) - (author: "Vincent Prouillet") - (about: "Static site generator") - (@setting SubcommandRequiredElseHelp) - (@arg config: -c --config +takes_value "Path to a config file other than config.toml") - (@subcommand init => - (about: "Create a new Gutenberg project") - (@arg name: +required "Name of the project. Will create a directory with that name in the current directory") - ) - (@subcommand build => - (about: "Builds the site") - ) - (@subcommand serve => - (about: "Serve the site. Rebuild and reload on change automatically") - (@arg interface: "Interface to bind on (default to 127.0.0.1)") - (@arg port: "Which port to use (default to 1111)") - ) - ).get_matches(); + let matches = cli::build_cli().get_matches(); let config_file = matches.value_of("config").unwrap_or("config.toml");