aboutsummaryrefslogtreecommitdiff
path: root/src/cliargs.rs
blob: 7fd512e57b01c8eef7436e44ac46e13215f2fe48 (plain)
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
98
// bibiman - a TUI for managing BibLaTeX databases
// Copyright (C) 2024  lukeflo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
/////

use sarge::prelude::*;
use std::env;
use std::path::PathBuf;

sarge! {
    // Name of the struct
    ArgumentsCLI,

    // Show help and exit.
    'h' help: bool,

    // Show version and exit.
    'v' version: bool,
}

// struct for CLIArgs
pub struct CLIArgs {
    pub helparg: bool,
    pub versionarg: bool,
    pub bibfilearg: PathBuf,
}

impl Default for CLIArgs {
    fn default() -> Self {
        Self::new()
    }
}

impl CLIArgs {
    pub fn new() -> Self {
        let (cli_args, pos_args) = ArgumentsCLI::parse().expect("Could not parse CLI arguments");
        let bibfilearg = if pos_args.len() > 1 {
            PathBuf::from(&pos_args[1])
            // pos_args[1].to_string()
        } else {
            PathBuf::new()
        };
        Self {
            helparg: cli_args.help,
            versionarg: cli_args.version,
            bibfilearg,
        }
    }
}

pub fn help_func() -> String {
    let help = format!(
        "\
{} {}

USAGE:
    bibiman [FLAGS] [file]

POSITIONAL ARGS:
    <file>    Path to .bib file

FLAGS:
    -h, --help      Show this help and exit
    -v, --version   Show the version and exit",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
    );
    help
}

pub fn version_func() -> String {
    let version = format!(
        "\
{} {}
{}
{}

Target Triple: {}",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
        env!("CARGO_PKG_LICENSE"),
        env!("TARGET")
    );
    version
}