-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
fix Issue 13290 - rdmd --eval ignores flags and program args #140
Conversation
This adds yet more arg handling logic to rdmd. Instead, it may be better to add some little features to std.getopt:
|
Do we really need |
This enables the alternative style of passing a value for an option: rdmd --eval 'writeln("Hello World!");' Before, the code would have been mistaken for the program file.
Made a PR for that: dlang/phobos#2429 Also, this now enables |
I think it would be an arbitrary restriction if --eval couldn't handle args. I don't see a reason against supporting it. |
I'm not sure, is there a point to writing things like:
as opposed to just:
? |
On Friday 15 August 2014 11:05:02 H. S. Teoh wrote:
There is a point when you're learning D, and want to figure out how that There may be a point when piecing together args is easier than piecing
I don't see a point in not supporting it. What's the downside? |
I dunno, if code gets to the point where you start needing to parse args, I'd argue that you should start using a source file instead of parsing a long list of options to |
Cost of maintenance. If someone will want to add something else to the argument parsing logic, the code's complexity will continue increasing. And if someone starts using the functionality and discovers a bug in it, there will need to be someone to understand the code and fix it. I'm not convinced about the std.getopt patch for mostly the same reasons, it seems to be a rather narrow use-case. |
I think that at the very least, it should be made possible to achieve what Nils wants to achieve in |
Fair enough, I can get behind the idea of keeping the user's options open, as long as the design is sound. |
On Friday 15 August 2014 13:18:03 Vladimir Panteleev wrote:
This PR decreases the complexity of rdmd's arg parsing, by letting getopt
They are very minor additions. But they do make getopt slightly more complex, The point is, without them, if rdmd was to have proper arg parsing ( And once rdmd has proper arg parsing, supporting --eval + args is trivial. I see three ways to go:
|
I split up https://issues.dlang.org/show_bug.cgi?id=13290 into three separate issues: The fix for 13344 doesn't require touching getopt. I made PR #143 for that. |
Here's another idea for making getopt flexible enough for rdmd: Have a function Usage in rdmd would look something like this: string[] compilerArgs;
string[] programFileAndArgs;
args.popFront(); // rdmd itself
while(!args.empty)
{
auto r = getOneOpt(args, /* etc */);
switch(r)
{
case GetOneOptResult.handledOption: /* ok, go on */ break;
case GetOneOptResult.notRecognized:
if(isProgram(args[0])) // everything from here is program + args
{
programFileAndArgs = args;
args = [];
}
else // flag or file for the compiler
{
compilerArgs ~= args[0];
args.popFront();
}
break;
case GetOneOptResult.endOfOptions: // everything from here is program + args
programFileAndArgs = args;
args = [];
break;
}
} Essentially, this gives the user a way to intervene on each argument. Allowing them to choose per argument to handle it themselves, or stop processing, or just let
Would this fly? |
I agree that the current implementation of getopt could use some improvement -- currently, it scans over the entire argument list per available option, which seems horribly inefficient for me. Of course, most of the time this doesn't matter since we're only dealing with a small number of arguments and a relatively small number of possible options. But in non-trivial CLI applications this cost could become prohibitive. I'm also not particularly pleased with the way I'm not so sure |
On Thursday 21 August 2014 07:37:18 H. S. Teoh wrote:
[...]
There may be a misunderstanding here. You'd still give
I'm hesitating to start from scratch, breaking everything.
I'm looking at http://perldoc.perl.org/Getopt/Long.html#Configuring-Getopt::Long, and as far as I can see, our getopt already tries to mimic that Also, I can't figure out how to implement rdmd's arg style with perl's getopt.
What's missing? And how is the current design in its way? Option descriptions can be passed along with the names. And getopt could do that itself and exit the program, but I guess it was a |
It's true that Sorry, gotta run, will type the rest of my reply later. |
Ping? |
Sorry, got busy with other stuff and forgot about this one. What I meant about Wrt the automated help, I was thinking more in terms of a more declarative approach, where user code would specify each option, its receiving variable, plus associated help text, and One approach, though, that doesn't require ground-up rewrite, is to define a special
Then But of course, this is an enhancement request beyond the scope of this PR. |
Ok, got it. I agree that passing the option spec over and over again is not ideal. The first thing I had thought of was to add a callback for non-options. I had dismissed that for some reason, but I can't see why now. It would signal if processing is to stop, and if the arg is to be removed from the array. It would be possible to expand on the idea of So, I'd go with the callback. And I think I'm running out of fuel if that's not good either.
You can already pass help text for options as part of the option spec: import std.getopt;
void main(string[] args)
{
int n;
float x;
auto r = getopt(args,
"n", "The number of iterations to run", &n,
"x", "The initial seed value for the algorithm", &x,
// ....
);
if(r.helpWanted)
defaultGetoptPrinter("Some information about the program.", r.options);
} Called with
By the way, could I get someone to review PR #143? It fixes the one issue I had originally hit. It doesn't require any changes to getopt, doesn't touch arg parsing at all. |
#143 is in, what's the status of this? |
The current code here is obsolete. I'd like to get a green light, or rather a second opinion, on the proposed addition to getopt before I get to work again. That is, how about adding a callback for non-options that
Option callbacks could get those abilities, too, of course. rdmd would then use that to stop on the first non-option argument, leaving it in the array. |
Any work on the underpowered getopt would be appreciated, but I agree, that passing arguments to eval is too much. |
Point is, when getopt gets that little extra power, --eval+args doesn't need any extra work. Then arguments for the program are simply what's behind getopt's stop position. |
https://issues.dlang.org/show_bug.cgi?id=13290https://issues.dlang.org/show_bug.cgi?id=13344 (but there is #143 for just this issue)
https://issues.dlang.org/show_bug.cgi?id=13345
https://issues.dlang.org/show_bug.cgi?id=13346
Depends on dlang/phobos#2429