aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: b714ce7f8dd04ddbdb54c78baea7f711101a8b30 (plain) (blame)
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
99
100
101
102
103
104
// This file is part of Chorus <https://github.com/Fuwn/chorus>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// 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, version 3.
//
// 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 <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use clap::{Parser, Subcommand};

use crate::commands;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
struct Chorus {
  #[clap(subcommand)]
  command: Commands,

  /// Enable debug output
  #[clap(short, long, parse(from_occurrences))]
  debug: usize,
}

#[derive(Subcommand)]
enum Commands {
  /// Create a new Chorus package in an existing directory
  Init {
    /// The location to create the new Chorus package at
    path: Option<std::path::PathBuf>,
  },
  /// Create a new Chorus package in a new directory
  New {
    /// The location to create the new Chorus project at
    path: std::path::PathBuf,
  },
  /// Format all COBOL files within a Chorus package
  Fmt {
    /// The location of the target Chorus package to format
    #[clap(short, long)]
    path: Option<std::path::PathBuf>,
  },
  /// Compile the Chorus package within the current directory
  Build {
    /// The location of the target Chorus package to build
    #[clap(short, long)]
    path:   Option<std::path::PathBuf>,
    #[clap(short, long)]
    output: Option<std::path::PathBuf>,
  },
}

/// Setup and handle command-line interactions
pub fn evaluate() {
  // Set up the CLI
  let chorus = Chorus::parse();

  // Handle CLI command
  match &chorus.command {
    Commands::Init {
      path,
    } => {
      if let Err(why) = commands::init_new(path, true) {
        println!("error: could not initialise package: {}", why);
        std::process::exit(1);
      };
    }
    Commands::New {
      path,
    } => {
      if let Err(why) = commands::init_new(&Some(path.clone()), false) {
        println!("error: could not create new package: {}", why);
        std::process::exit(1);
      };
    }
    Commands::Fmt {
      path,
    } => {
      if let Err(why) = commands::format(path) {
        println!("error: could not format package: {}", why);
        std::process::exit(1);
      };
    }
    Commands::Build {
      path,
      output,
    } => {
      if let Err(why) = commands::build(path, output) {
        println!("error: could not build package: {}", why);
        std::process::exit(1);
      };
    }
  }
}