1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2020 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use std::env;
use std::ffi::OsString;
use std::process;
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
pub struct Cli {
    /// Period to sleep between executions
    #[structopt(short, long, default_value = "1")]
    pub period: u64,

    /// Invoke the shell on the single command argument
    #[structopt(short = "c", long = "shell")]
    pub shell: bool,

    /// Loop until the command exists with success
    #[structopt(short = "z", long = "until-success")]
    pub until_success: bool,

    /// The command to run
    pub command: Vec<String>,
}

impl Cli {
    pub fn from_args() -> Cli {
        Cli::from_iter_safe(&mut env::args_os()).unwrap_or_else(|e| {
            eprintln!("{}", e.message);
            process::exit(1);
        })
    }

    pub fn from_iter_safe<I>(iter: I) -> Result<Cli, clap::Error>
    where
        Self: Sized,
        I: IntoIterator,
        I::Item: Into<OsString> + Clone,
    {
        let clap = Cli::clap().get_matches_from_safe(iter)?;
        let cli = Cli::from_clap(&clap);
        if cli.command.is_empty() {
            Err(clap::Error::with_description(
                "No command specified

For more information try --help",
                clap::ErrorKind::EmptyValue,
            ))
        } else if cli.shell && cli.command.len() != 1 {
            Err(clap::Error::with_description(
                "In shell mode, command must be in a single argument

For more information try --help",
                clap::ErrorKind::InvalidValue,
            ))
        } else {
            Ok(cli)
        }
    }
}

#[test]
fn empty() {
    let cli = Cli::from_iter_safe(vec!["ogle"]);
    assert!(cli.is_err());
}

#[test]
fn dashes() {
    let cli = Cli::from_iter_safe(vec!["ogle", "--"]);
    assert!(cli.is_err());
    let cli = Cli::from_iter_safe(vec!["ogle", "--", "ls", "-l"]).unwrap();
    assert_eq!(cli.command[0], "ls");
    assert_eq!(cli.command[1], "-l");
    assert_eq!(cli.command.len(), 2);
    assert_eq!(cli.period, 1);
}

#[test]
fn period() {
    let cli = Cli::from_iter_safe(vec!["ogle", "-p", "5", "--", "ls", "-l"]).unwrap();
    assert_eq!(cli.period, 5);
    let cli = Cli::from_iter_safe(vec!["ogle", "--period", "7", "--", "ls", "-l"]).unwrap();
    assert_eq!(cli.period, 7);
}

#[test]
fn shell() {
    let cli = Cli::from_iter_safe(vec!["ogle", "-c", "--", "ls", "-l"]);
    assert!(cli.is_err());
    let cli = Cli::from_iter_safe(vec!["ogle", "-c", "--", "ls -l"]).unwrap();
    assert!(cli.shell);
    assert_eq!(cli.command[0], "ls -l");
    assert_eq!(cli.command.len(), 1);
}