Improve CLI
This commit is contained in:
parent
299610e16f
commit
85e1b5cff2
@ -12,7 +12,6 @@ use log::{Level, LevelFilter};
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
author = env!("CARGO_PKG_AUTHORS"),
|
||||
about = env!("CARGO_PKG_DESCRIPTION"),
|
||||
|
||||
)]
|
||||
#[command(args_conflicts_with_subcommands = true)]
|
||||
struct Cli {
|
||||
@ -62,21 +61,24 @@ enum CliMode {
|
||||
#[derive(Args, Clone)]
|
||||
struct GlobalArguments {
|
||||
/// Log level, overrides the DUST_LOG environment variable
|
||||
///
|
||||
/// Possible values: trace, debug, info, warn, error
|
||||
#[arg(short, long, value_name = "LOG_LEVEL")]
|
||||
log: Option<LevelFilter>,
|
||||
|
||||
/// Source code sent via command line
|
||||
#[arg(short, long, value_name = "SOURCE", conflicts_with = "path")]
|
||||
/// Source code
|
||||
///
|
||||
/// Conflicts with the file argument
|
||||
#[arg(short, long, value_name = "SOURCE", conflicts_with = "file")]
|
||||
command: Option<String>,
|
||||
|
||||
/// File to read source code from
|
||||
/// Path to a source code file
|
||||
#[arg(required_unless_present = "command")]
|
||||
path: Option<PathBuf>,
|
||||
file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl GlobalArguments {
|
||||
fn set_log_and_get_source(self, start_time: Instant) -> String {
|
||||
let GlobalArguments { command, path, log } = self;
|
||||
fn set_log_and_get_source(arguments: GlobalArguments, start_time: Instant) -> String {
|
||||
let GlobalArguments { command, file, log } = arguments;
|
||||
let mut logger = env_logger::builder();
|
||||
|
||||
logger.format(move |buf, record| {
|
||||
@ -102,12 +104,11 @@ impl GlobalArguments {
|
||||
if let Some(source) = command {
|
||||
source
|
||||
} else {
|
||||
let path = path.expect("Path is required when command is not provided");
|
||||
let path = file.expect("Path is required when command is not provided");
|
||||
|
||||
read_to_string(path).expect("Failed to read file")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let start_time = Instant::now();
|
||||
@ -125,7 +126,7 @@ fn main() {
|
||||
no_output,
|
||||
} = mode
|
||||
{
|
||||
let source = global_arguments.set_log_and_get_source(start_time);
|
||||
let source = set_log_and_get_source(global_arguments, start_time);
|
||||
let run_result = run(&source);
|
||||
|
||||
match run_result {
|
||||
@ -148,7 +149,7 @@ fn main() {
|
||||
style,
|
||||
} = mode
|
||||
{
|
||||
let source = global_arguments.set_log_and_get_source(start_time);
|
||||
let source = set_log_and_get_source(global_arguments, start_time);
|
||||
let chunk = match compile(&source) {
|
||||
Ok(chunk) => chunk,
|
||||
Err(error) => {
|
||||
@ -169,7 +170,7 @@ fn main() {
|
||||
style,
|
||||
} = mode
|
||||
{
|
||||
let source = global_arguments.set_log_and_get_source(start_time);
|
||||
let source = set_log_and_get_source(global_arguments, start_time);
|
||||
let tokens = match lex(&source) {
|
||||
Ok(tokens) => tokens,
|
||||
Err(error) => {
|
||||
|
Loading…
Reference in New Issue
Block a user