Skip to content

Commit

Permalink
Generate command line completions in a build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Soft committed Mar 23, 2018
1 parent 29713c1 commit 795ba3b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ nom = "3.2.1"
clap = "2.31.2"
nix = "0.10.0"

[build-dependencies]
clap = "2.31.2"

[profile.release]
lto = true
panic = "abort"
Expand Down
13 changes: 13 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate clap;

use std::env;
use clap::Shell;

include!("src/cli.rs");

fn main() {
let mut app = get_cli();
for shell in [Shell::Bash, Shell::Fish, Shell::Zsh].into_iter() {
app.gen_completions("xcolor", *shell, env::var("OUT_DIR").unwrap());
}
}
42 changes: 42 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use clap::{App, Arg};

pub fn get_cli() -> App<'static, 'static> {
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name("format")
.short("f")
.long("format")
.takes_value(true)
.value_name("NAME")
.help("Output format (defaults to hex)")
.possible_values(&["hex", "HEX", "plain", "rgb"])
.conflicts_with("custom"))
.arg(Arg::with_name("custom")
.short("c")
.long("custom")
.takes_value(true)
.value_name("FORMAT")
.help("Custom output format")
.conflicts_with("format"))
.arg(Arg::with_name("selection")
.short("s")
.long("selection")
.takes_value(true)
.value_name("SELECTION")
.min_values(0)
.max_values(1)
.possible_values(&["primary", "secondary"])
.help("Output to selection (defaults to primary)"));

if cfg!(debug_assertions) {
app = app.arg(Arg::with_name("foreground")
.short("F")
.long("foreground")
.requires("selection")
.help("Stay in the foreground"));
}

app
}
45 changes: 3 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,16 @@ extern crate nom;
mod format;
mod x11;
mod selection;
mod cli;

use failure::{Error, err_msg};
use xcb::base::Connection;
use clap::{App, Arg, ArgMatches};
use clap::ArgMatches;
use nix::unistd::ForkResult;

use format::{Format, FormatString, FormatColor};
use selection::{Selection, daemonize, set_selection};

fn get_cli() -> App<'static, 'static> {
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name("format")
.short("f")
.long("format")
.takes_value(true)
.value_name("NAME")
.help("Output format (defaults to hex)")
.possible_values(&["hex", "HEX", "plain", "rgb"])
.conflicts_with("custom"))
.arg(Arg::with_name("custom")
.short("c")
.long("custom")
.takes_value(true)
.value_name("FORMAT")
.help("Custom output format")
.conflicts_with("format"))
.arg(Arg::with_name("selection")
.short("s")
.long("selection")
.takes_value(true)
.value_name("SELECTION")
.min_values(0)
.max_values(1)
.possible_values(&["primary", "secondary"])
.help("Output to selection (defaults to primary)"));

if cfg!(debug_assertions) {
app = app.arg(Arg::with_name("foreground")
.short("F")
.long("foreground")
.requires("selection")
.help("Stay in the foreground"));
}

app
}
use cli::get_cli;

fn run<'a>(args: ArgMatches<'a>) -> Result<(), Error> {
fn error(message: &str) -> ! {
Expand Down

0 comments on commit 795ba3b

Please sign in to comment.