-
Notifications
You must be signed in to change notification settings - Fork 94
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
Implement command line parser builtin #663
Open
hdwalters
wants to merge
11
commits into
amber-lang:staging
Choose a base branch
from
hdwalters:implement-parser-builtin
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
17c8854
Import regex crate.
hdwalters c6d9fb3
Allow hyphen trailing var args.
hdwalters fba5148
Add boolean integer conversion.
hdwalters 7b2a68a
Add variable declaration payload.
hdwalters ff8d445
Parse command line syntax.
hdwalters 61c5706
Translate command line code.
hdwalters be01158
Use Amber script name in parser help text.
hdwalters ac6724f
Add validity integration tests.
hdwalters 78028fb
Make Clippy happy.
hdwalters 63330fe
Allow hyphenated long options.
hdwalters 2645d7c
Generate getopt variable name.
hdwalters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::docs::module::DocumentationModule; | ||
use crate::modules::builtin::cli::param::ParamImpl; | ||
use crate::modules::builtin::cli::parser::ParserImpl; | ||
use crate::modules::expression::expr::Expr; | ||
use crate::modules::types::{Type, Typed}; | ||
use crate::translate::module::TranslateModule; | ||
use crate::utils::metadata::{ParserMetadata, TranslateMetadata}; | ||
use heraclitus_compiler::prelude::*; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct GetoptCli { | ||
parser: Option<Rc<RefCell<ParserImpl>>>, | ||
args: Box<Expr>, | ||
} | ||
|
||
impl Typed for GetoptCli { | ||
fn get_type(&self) -> Type { | ||
Type::Null | ||
} | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for GetoptCli { | ||
syntax_name!("Getopt Invocation"); | ||
|
||
fn new() -> Self { | ||
let args = Box::new(Expr::new()); | ||
Self { parser: None, args } | ||
} | ||
|
||
fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
token(meta, "getopt")?; | ||
let mut parser = Expr::new(); | ||
token(meta, "(")?; | ||
let parser_tok = meta.get_current_token(); | ||
syntax(meta, &mut parser)?; | ||
token(meta, ",")?; | ||
syntax(meta, &mut *self.args)?; | ||
token(meta, ")")?; | ||
let parser = match ParserImpl::find_parser(meta, &parser) { | ||
Some(parser) => parser, | ||
None => return error!(meta, parser_tok, "Expected parser object"), | ||
}; | ||
parser.borrow_mut().add_param(ParamImpl::help()); | ||
self.parser = Some(parser); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TranslateModule for GetoptCli { | ||
fn translate(&self, meta: &mut TranslateMetadata) -> String { | ||
self.parser.as_ref() | ||
.map(|parser| parser.borrow().translate(meta, &self.args)) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
impl DocumentationModule for GetoptCli { | ||
fn document(&self, _meta: &ParserMetadata) -> String { | ||
String::new() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod getopt; | ||
pub mod param; | ||
pub mod parser; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows long options
--xyz
and short options-x
to be passed to the Amber script, without being rejected as invalid by theclap
library; butclap
still consumes the--help
option withamber script.ab --help
or./script.ab --help
. I would like to make it pass all options following a positional argument to the Amber script, but I'm not sure how. All suggestions appreciated!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is of course possible to force options to be passed to the script with
amber script.ab -- --help
. I've tried moving this Clap argument to the end of the struct and adding#[arg(last = true)]
, but that makes it necessary to callamber script.ab -- arg1 arg2
for all arguments, not just--help
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hdwalters does
allow_external_subcommands
solve this issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it would. According to the docs, it handles "unexpected positional arguments" (such as
foo
) not optional ones (such as--help
).I will try it, however.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may have to put up with
./script.ab -- --help
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... I think that this solution of adding the
--
is good enough. It adds the separation between the Amber's arguments and the script args.