reups_lib/
argparse.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 * Copyright Nate Lust 2018*/
5
6#[doc(no_inline)]
7pub use clap::ArgMatches;
8use clap::{App, Arg, SubCommand};
9
10/**
11 * Builds and returns the sub command struct, containing all the options for the setup command
12*/
13pub fn build_setup<'a, 'b>() -> App<'a, 'b> {
14    return SubCommand::with_name("setup")
15        .arg(Arg::with_name("product").help("Product to setup").index(1))
16        .arg(
17            Arg::with_name("just")
18                .help("ignore dependncies")
19                .short("j")
20                .long("just"),
21        )
22        .arg(
23            Arg::with_name("relative")
24                .help("setup relative path")
25                .short("r")
26                .long("relative")
27                .takes_value(true),
28        )
29        .arg(
30            Arg::with_name("keep")
31                .help("keep exsisting setup products")
32                .short("k")
33                .long("keep"),
34        )
35        .arg(
36            Arg::with_name("tag")
37                .help("specify one or more tags to look up for products, evaluated left to right")
38                .short("t")
39                .long("tag")
40                .multiple(true)
41                .number_of_values(1)
42                .takes_value(true),
43        )
44        .arg(
45            Arg::with_name("inexact")
46                .help("Run setup with Inexact versions as specified in the table files")
47                .short("E")
48                .long("inexact"),
49        )
50        .arg(
51            Arg::with_name("verbose")
52                .short("v")
53                .long("verbose")
54                .multiple(true)
55                .help("Sets the level of verbosity, multiple occurances increases verbosity"),
56        );
57}
58
59/**
60 * Builds and returns the sub command struct, containing all the options for the list command
61 */
62fn build_list<'a, 'b>() -> App<'a, 'b> {
63    return SubCommand::with_name("list")
64                          .arg(Arg::with_name("product")
65                               .help("Name of product to list (optional)")
66                               .index(1)
67                               .conflicts_with_all(&["setup", "local"]))
68                          .arg(Arg::with_name("setup")
69                               .help("List only setup products")
70                               .short("s")
71                               .long("setup")
72                               .conflicts_with_all(&["product", "local"]))
73                          .arg(Arg::with_name("tags")
74                               .help("List only these tags (does not include current)")
75                               .short("t")
76                               .long("tags")
77                               .multiple(true)
78                               .number_of_values(1)
79                               .takes_value(true))
80                          .arg(Arg::with_name("onlyTags")
81                               .help("Only list product & tags (faster than tags and versions, but does not indicate setup tag)")
82                               .long("onlyTags"))
83                          .arg(Arg::with_name("onlyVers")
84                               .help("Only list product & versions (faster than tags and versions)")
85                               .long("onlyVers")
86                               .conflicts_with("onlyTags"))
87                          .arg(Arg::with_name("local")
88                               .help("Only list products that are setup as local products")
89                               .short("l")
90                               .long("local")
91                               .conflicts_with_all(&["product", "setup"]));
92}
93
94/**
95 * Builds and returns the sub command struct, containing all the options for the completions command.
96 */
97fn build_completions<'a, 'b>() -> App<'a, 'b> {
98    return SubCommand::with_name("completions")
99        .about("Creates auto completeion scripts for given shell")
100        .arg(
101            Arg::with_name("shell")
102                .required(true)
103                .possible_values(&["bash", "fish", "zsh", "elvish"])
104                .help("Shell to create completions for"),
105        );
106}
107
108/**
109 * Builds the completetions for the sub command env. This allows the reups commands run in one
110 * shell to be recored and replayed in another shell
111 */
112fn build_env<'a, 'b>() -> App<'a, 'b> {
113    return SubCommand::with_name("env")
114        .about("Save or restore an existing reups environment")
115        .arg(
116            Arg::with_name("command")
117                .required(true)
118                .possible_values(&["save", "restore", "delete", "list"])
119                .help("Action to take for a given environment, to restore you most likely want to use the rrestore shell function"),
120        )
121        .arg(
122            Arg::with_name("name")
123                .required(false)
124                .help("Optional name to save/restore"),
125        )
126        .arg(
127            Arg::with_name("verbose")
128                .short("v")
129                .long("verbose")
130                .multiple(true)
131                .help("Sets the level of verbosity, multiple occurances increases verbosity"),
132        );
133}
134
135/**
136 * Builds and returns the sub command struct, containing all the options for the prep command.
137 *
138 * Currently this is basically an empty command just to create the setup option, but in the future
139 * this command may take optional arguments such as a configuration file to use in preping.
140 */
141fn build_prep<'a, 'b>() -> App<'a, 'b> {
142    return SubCommand::with_name("prep");
143}
144
145/**
146 * This function is responsible for creating all the possible command line options and arguments for the main program, and each of the sub commands.
147 */
148pub fn build_cli() -> App<'static, 'static> {
149    App::new("Rust Eups")
150        .author("Nate Lust")
151        .about("Dynamic environment management")
152        .version(clap::crate_version!())
153        .subcommand(build_setup())
154        .subcommand(build_prep())
155        .subcommand(build_list())
156        .subcommand(build_completions())
157        .subcommand(build_env())
158}
159
160/**
161 * This is the main argument parser for the reups program. It parses the arguments from the command
162 * line into a `ArgMatches` object containing all the supplied options.
163 */
164pub fn parse_args<'a>() -> ArgMatches<'a> {
165    let matches = build_cli().get_matches();
166    return matches;
167}