Skip to content

Commit

Permalink
Refactor the Nickel CLI (#1502)
Browse files Browse the repository at this point in the history
* Refactor the Nickel CLI structure

* Add an `Error` enum for the CLI

* Restore the previous user-facing interface

* Make clippy happy

* Restore `nickel format` CLI behaviour

* Change command options naming

* Properly respect feature flags

* WithProgram -> ReportWithProgram

* Remove the unecessary `Files` struct for now

* ReportWithProgram -> ResultErrorExt

* Suggestions from code review
  • Loading branch information
vkleen authored Aug 8, 2023
1 parent 943430e commit 84da9e7
Show file tree
Hide file tree
Showing 18 changed files with 593 additions and 384 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ assert_cmd = "2.0.11"
assert_matches = "1.5.0"
atty = "0.2.14"
clap = "4.3"
clap_complete = "4.3.2"
codespan = "0.11"
codespan-lsp = "0.11"
codespan-reporting = "0.11"
Expand All @@ -47,6 +48,7 @@ csv = "1"
derive_more = "0.99"
directories = "4.0.1"
env_logger = "0.10"
git-version = "0.3.5"
indexmap = "1.9.3"
indoc = "2"
insta = "1.29.0"
Expand Down Expand Up @@ -86,7 +88,6 @@ toml = "0.7.2"
typed-arena = "2.0.2"
unicode-segmentation = "1.10.1"
void = "1"
git-version = "0.3.5"

topiary = { version = "0.2.3", git = "https://github.com/tweag/topiary.git", rev = "refs/heads/main" }
# This should be kept in sync with the revision in topiary
Expand Down
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme.workspace = true

[[bin]]
name = "nickel"
path = "bin/nickel.rs"
path = "src/main.rs"
bench = false

[features]
Expand All @@ -33,6 +33,7 @@ tree-sitter-nickel = { workspace = true, optional = true }
tempfile = { workspace = true, optional = true }

git-version = { workspace = true }
clap_complete = { workspace = true }

[dev-dependencies]
nickel-lang-utils.workspace = true
Expand Down
248 changes: 0 additions & 248 deletions cli/bin/nickel.rs

This file was deleted.

80 changes: 80 additions & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! Command-line options and subcommands.
use std::path::PathBuf;

use git_version::git_version;

use crate::{
completions::GenCompletionsCommand, eval::EvalCommand, export::ExportCommand,
pprint_ast::PprintAstCommand, query::QueryCommand, typecheck::TypecheckCommand,
};

#[cfg(feature = "repl")]
use crate::repl::ReplCommand;

#[cfg(feature = "doc")]
use crate::doc::DocCommand;

#[cfg(feature = "format")]
use crate::format::FormatCommand;

#[derive(clap::Parser, Debug)]
/// The interpreter of the Nickel language.
#[command(
author,
about,
long_about = None,
version = format!("{} {} (rev {})", env!("CARGO_BIN_NAME"), env!("CARGO_PKG_VERSION"), git_version!(fallback = env!("NICKEL_NIX_BUILD_REV")))
)]
pub struct Options {
#[command(flatten)]
pub global: GlobalOptions,

#[command(subcommand)]
pub command: Option<Command>,
}

#[derive(clap::Parser, Debug)]
pub struct GlobalOptions {
#[cfg(debug_assertions)]
/// Skips the standard library import. For debugging only. This does not affect REPL
#[arg(long, global = true)]
pub nostdlib: bool,

/// Configure when to output messages in color
#[arg(long, global = true, value_enum, default_value_t)]
pub color: clap::ColorChoice,

/// Input file, omit to read from stdin
#[arg(long, short, global = true)]
pub file: Option<PathBuf>,
}

/// Available subcommands.
#[derive(clap::Subcommand, Debug)]
pub enum Command {
/// Evaluate a Nickel program and pretty-print the result.
#[command(hide = true)]
Eval(EvalCommand),

/// Converts the parsed representation (AST) back to Nickel source code and prints it. Used for
/// debugging purpose
PprintAst(PprintAstCommand),
/// Exports the result to a different format
Export(ExportCommand),
/// Prints the metadata attached to an attribute, given as a path
Query(QueryCommand),
/// Typechecks the program but do not run it
Typecheck(TypecheckCommand),
/// Starts a REPL session
#[cfg(feature = "repl")]
Repl(ReplCommand),
/// Generates the documentation files for the specified nickel file
#[cfg(feature = "doc")]
Doc(DocCommand),
/// Format Nickel files
#[cfg(feature = "format")]
Format(FormatCommand),

/// Generate shell completion files
GenCompletions(GenCompletionsCommand),
}
Loading

0 comments on commit 84da9e7

Please sign in to comment.