-
Notifications
You must be signed in to change notification settings - Fork 683
Add lightweight command line option processor to jerry-main #1809
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
Conversation
We should wait landing #1797 first. |
On the contrary, I'd like to see this landed first. The CLI of our C tools is less than professional, and the snapshot merging tool just continues that bad tradition. I'd like to see the new tool use proper command line option handling, too. |
The command line parsing in that patch is correct, and it would be better if this patch would contain all changes related to moving to a new method. I would rather avoid a situation when that patch needs to rework command line parsing again. |
Is this a binary size optimization? |
We disagree in that regard. It arbitrarily differs from the other tools we are using/writing. (git is not a right analogy for that tool, but I'll comment there, too.) I'm happy to show how to adapt to this approach and get a more flexible, easier to use, more intuitive CLI.
No (or, at least, I don't know). As far as I know, we don't care about the size of
into this
|
I see. Good patch. Why don't have any description for |
Now, I have to reveal that I'm not sure what they do exactly (or rather, what the exact difference between the variants is). I'd be happy to add the help messages if someone sent them to me (PM is fine). |
--save-snapshot-for-global FILE |
jerry-main/cli.c
Outdated
{ | ||
for (int i = 0; i < cnt; i++) | ||
{ | ||
printf (" "); |
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.
Just a nitpick: can't we just do this?: printf("%*s", cnt, " ");
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, but I think jerry-libc does not support it.
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.
Unfortunately, no. jerry-libc doesn't support *
width specifier
Thanks for helping me out with the help strings. PR updated |
e2923b0
to
42474f8
Compare
PR is updated (option help message line wrapping added + some minor light touches). current output is:
|
GCC says
I think a similar approach is enough for us. |
@zherczeg but compare to our other tools |
d2d83fa
to
59e4ef2
Compare
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.
Unfortunately this is very complex for me. I really have no idea how to add a new argument without your help, which is kind of a maintenance risk. Especially because there is no documentation. But it blocks other work so lets land it.
jerry-main/cli.c
Outdated
{ | ||
int length = -1; | ||
int i = 0; | ||
for (; i < CLI_LINE_LENGTH - CLI_LINE_TAB && help[i] != 0; i++) |
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.
for (int i = 0;
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.
cannot do that, i
is used after the loop
int argc, /**< number of command line arguments */ | ||
char **argv) /**< array of command line arguments */ | ||
{ | ||
return (cli_opt_state_t) |
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.
Returning with structures is always costly. It is better to pass a pointer.
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.
On many platforms, this is exactly what happens when returning structs. AFAIK both on ARM32 and X86, struct returns are done by passing a hidden pointer argument to the function, where it has to write the results. So, x = f();
and f(&x);
are exactly the same. (The former might work better with optimizations, as the ABI-enforced extra pointer argument might be easier to track in pointer aliasing algorithms than completely generic pointer arguments, but that can be use case specific, whether there is a real difference).
(BTW, this is not performance-critical code (even if there would be any performance penalty). This function is typically called only once in an application. And this is for jerry-main only, not for anyone to use.)
jerry-main/cli.c
Outdated
void | ||
cli_opt_help (const cli_opt_t *opts) /**< array of command line option definitions, terminated by CLI_OPT_END */ | ||
{ | ||
for (; opts->id != CLI_OPT_END; opts++) |
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 should be a while loop.
CLI_QUANT_Q, /**< ? (Question mark: optional, zero or one) */ | ||
CLI_QUANT_A, /**< * (Asterisk, star: zero or one or more) */ | ||
CLI_QUANT_P, /**< + (Plus sign: one or more) */ | ||
CLI_QUANT_1 /**< 1 (One: one) */ |
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.
What about two argument.
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.
No need for that, as there is no use case other than these, which could be supported easily. Note that the quantifiers are for display only, not enforced in any other way.
?
gets displayed as[-x X]
or[X]
*
gets displayed as[-x X]...
or[X]...
+
gets displayed as-x X...
orX...
1
gets displayed as-x X
orX
There is no easy or conventional notation for more general quantifiers (like min 3, max infinity) for command line arguments, and I'm not sure we'd need them. (This is not a general purpose CLI library, it's for the needs of jerry-main only.)
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.
(Just don't forget that the snapshot merging patch requires an optional argument with two items, a name and an index)
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.
That's not the same, that's what .argc
is for. .quant
describes whether and option (together with its argument(s)) is optional or not (or how many times it should appear on the command line). E.g., --exec-snapshot snapfile1 --exec-snapshot snapfile2 --exec-snapshot snapfile3
is a valid use, that's why we have [--exec-snapshot FILE]...
in the usage. For the snapshot merging patch, another option is to be added, used as --exec-snapshot-func snapfile1 index1
IIRC, which means that it has two arguments, but the option together with its two arguments may also appear arbitrarily many times on the command line (.argc = 2, .meta = "FILE NUM", .quant = CLI_QUANT_A
), displayed in usage as [--exec-snapshot-func FILE NUM]...
.
c4523f7
to
e6a01ec
Compare
I've updated the PR and rebased it to latest master. |
@@ -2,4 +2,4 @@ wrongmathcall:tests/unit-libm/test-libm.inc.h | |||
variableScope:jerry-libm/*.c | |||
invalidPointerCast:jerry-libm/*.c | |||
arithOperationsOnVoidPointer:jerry-libc/*.c | |||
commaSeparatedReturn:jerry-ext/include/jerryscript-ext/arg.impl.h | |||
commaSeparatedReturn:* |
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.
Could you explain this change? I'm not sure suppressing a warning in the whole code base is a good idea.
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.
Cppcheck warns on every struct literal in returns, because it misinterprets them as comma-separated expressions. This is quite bad IMO and obviously a shortcoming of cppcheck, because struct literals are completely valid C constructs.
jerry-main/main-unix.c
Outdated
OPT_LOG_LEVEL, | ||
OPT_ABORT_ON_FAIL, | ||
OPT_NO_PROMPT | ||
} jerry_opt_id_t; |
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.
Based on the previous conversation on the naming conventions, I'd rename this to cli_opt_id_t
. jerry_
prefix should be reserved for API functions and types.
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.
what about main_opt_id_t ? (I'd keep cli_ for cli.{c,h})
I've rebased the PR to latest master, resolved conflicts, and renamed variables and types in main-unix.c as requested |
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.
LGTM
Eases command line option and sub-command definition, usage summary and detailed help message printing, and argv processing. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
REPL could read JS file from standard input when we passed "-" option, but jerryscript-project#1809 missed to add this function to the new option parser. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác [email protected]
Eases command line option definition, usage summary and detailed
help message printing, and option processing.
JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]