Generate completions for all shells

This commit is contained in:
Vincent Prouillet 2017-07-15 13:24:31 +09:00
parent fbe34fd73d
commit 2ea77f3883
8 changed files with 369 additions and 19 deletions

View file

@ -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"

15
build.rs Normal file
View file

@ -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/");
}

101
completions/_gutenberg Normal file
View file

@ -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 "$@"

View file

@ -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', $_ }
}
}

View file

@ -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=("<config>")
return 0
;;
-c)
COMPREPLY=("<config>")
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 <name> "
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 <interface> <port> "
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

View file

@ -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"

24
src/cli.rs Normal file
View file

@ -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)")
)
)
}

View file

@ -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");