Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CLI structure and add new command #891

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 48 additions & 32 deletions shards/lang/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::Error;
use crate::read::{get_dependencies, read_with_env, ReadEnv};
use crate::{eval, formatter};
use crate::{eval::eval, eval::new_cancellation_token, read::read};
use clap::{arg, Arg, ArgMatches, Command, Parser};
use clap::Parser;
use shards::core::Core;
use shards::types::Mesh;
use shards::{fourCharacterCode, shlog, shlog_error, SHCore, SHARDS_CURRENT_ABI};
Expand All @@ -20,6 +20,18 @@ extern "C" {
fn shardsInterface(version: u32) -> *mut SHCore;
}

#[derive(clap::Args, Debug, Clone)]
struct RunCmd {
/// The script to execute
#[arg(value_hint = clap::ValueHint::FilePath, default_value = "autoexec.shs")]
file: String,
/// Decompress help strings before running the script
#[arg(long, short = 'd', default_value = "false", action)]
decompress_strings: bool,
#[arg(num_args = 0..)]
args: Vec<String>,
}

#[derive(Debug, clap::Subcommand)]
enum Commands {
/// Formats a shards file
Expand All @@ -31,23 +43,19 @@ enum Commands {
/// by default the output will go to stdout
#[arg(long, short = 'i', action)]
inline: bool,
/// Optinally an output file name
/// Optionally an output file name
#[arg(long, short = 'o')]
output: Option<String>,
},
/// Run formatter tests
Test {},
/// Reads and executes a Shards file
New {
/// The script to execute
#[arg(value_hint = clap::ValueHint::FilePath)]
file: String,
/// Decompress help strings before running the script
#[arg(long, short = 'd', default_value = "false", action)]
decompress_strings: bool,
/// Run old mal/clojure parser/evaluator mode
Old {
#[arg(num_args = 0..)]
args: Vec<String>,
_args: Vec<String>,
},
/// Reads and executes a Shards file
New(RunCmd),
/// Reads and builds a binary AST Shards file
Build {
/// The script to evaluate
Expand Down Expand Up @@ -82,18 +90,18 @@ enum Commands {
#[arg(num_args = 0..)]
args: Vec<String>,
},

#[command(external_subcommand)]
External(Vec<String>),
}

#[derive(Debug, clap::Parser)]
#[command(name = "Shards", version = "0.1")]
#[command(about = "Shards command line tools and executor.")]
#[command(author = "Fragcolor Team")]
#[command(args_conflicts_with_subcommands = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
command: Option<Commands>,
#[clap(flatten)]
run_cmd: RunCmd,
}

#[no_mangle]
Expand Down Expand Up @@ -130,39 +138,47 @@ pub extern "C" fn shards_process_args(

// Init Core interface when not running external commands
match &cli.command {
Commands::External(_) => {}
Some(Commands::Old { _args }) => {}
_ => unsafe {
shards::core::Core = shardsInterface(SHARDS_CURRENT_ABI as u32);
},
};

let res: Result<_, Error> = match &cli.command {
Commands::Build {
Some(Commands::Build {
file,
output,
depfile,
json,
} => build(file, &output, depfile.as_deref(), *json),
Commands::AST { file, output } => build(file, &output, None, true),
Commands::Load {
file,
decompress_strings,
args,
} => load(file, args, *decompress_strings, cancellation_token),
Commands::New {
}) => build(file, &output, depfile.as_deref(), *json),
Some(Commands::AST { file, output }) => build(file, &output, None, true),
Some(Commands::Load {
file,
decompress_strings,
args,
} => execute(file, *decompress_strings, args, cancellation_token),
Commands::Format {
}) => load(file, args, *decompress_strings, cancellation_token),
Some(Commands::New(run_cmd)) => execute(
&run_cmd.file,
run_cmd.decompress_strings,
&run_cmd.args,
cancellation_token,
),
Some(Commands::Format {
file,
output,
inline,
} => format(file, output, *inline),
Commands::Test {} => formatter::run_tests(),
Commands::External(_args) => {
}) => format(file, output, *inline),
Some(Commands::Test {}) => formatter::run_tests(),
Some(Commands::Old { _args }) => {
shlog!("Old mode is deprecated, please use the new mode");
return 99;
}
None => execute(
&cli.run_cmd.file,
cli.run_cmd.decompress_strings,
&cli.run_cmd.args,
cancellation_token,
),
};

if let Err(e) = res {
Expand Down Expand Up @@ -213,7 +229,7 @@ fn format(file: &str, output: &Option<String>, inline: bool) -> Result<(), Error
fn load(
file: &str,
args: &Vec<String>,
decompress_strings: bool,
_decompress_strings: bool,
cancellation_token: Arc<AtomicBool>,
) -> Result<(), Error> {
shlog!("Loading file");
Expand Down Expand Up @@ -377,7 +393,7 @@ fn build(file: &str, output: &str, depfile: Option<&str>, as_json: bool) -> Resu

fn execute(
file: &str,
decompress_strings: bool,
_decompress_strings: bool,
args: &Vec<String>,
cancellation_token: Arc<AtomicBool>,
) -> Result<(), Error> {
Expand Down
16 changes: 7 additions & 9 deletions shards/mal/stepA_mal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ int main(int argc, const char *argv[]) {
if (result != 99) // 99 triggers our old main
return result;

// we need to skip the second arg in this compatibility mode if second arg is `--`
if (argc > 2 && strcmp(argv[1], "--") == 0) {
argc--;
auto argv2 = argv + 1;
argv2[0] = argv[0]; // replace argv[1] with argv[0]
return malmain(argc, argv2);
} else {
return malmain(argc, argv);
}
SHLOG_DEBUG("Mal main");

// we need to skip the second arg
argc--;
auto argv2 = argv + 1;
argv2[0] = argv[0]; // replace argv[1] with argv[0]
return malmain(argc, argv2);
}

#endif
Expand Down
Loading