echo: "-n", the next installment

John Colvin john.loughran.colvin at gmail.com
Sat Jul 27 06:00:39 PDT 2013


On Saturday, 27 July 2013 at 12:52:11 UTC, monarch_dodra wrote:
> And here is the second comment I wanted to put:
>
> When parsing the options, you use an if-else. I don't know if 
> its just me, but I find that using a switch is clearer (it's 
> what you do in your second example). It also introduces string 
> cases (illegal in C++), and labeled control statements.
>
> The code becomes:
>
> import std.stdio : writef, writeln;
> void main(string[] args)
> {
>     assert(args.length);
>     args = args[1 .. $];
>     bool writeNewline = true;
>
>     size_t i = 0;
>     myForeach: foreach(arg; args)
>     {
>         switch(arg)
>         {
>             case "-n":
>                 writeNewline = false;
>                 ++i;
>                 break;
>
>             default:
>                 break myForeach;
>         }
>     }
>     args = args[i .. $];
>
>     writef("%-(%s %)", args);
>     if(writeNewline)
>         writeln();
> }
>
> PS: If I figure out how to comment on your blog, I'll paste the 
> comments there :)

thanks for the info on writef, I didn't realise it could do that.

About the switch statement: I didn't even know you could do that 
with labels, I'd only ever used them for gotos. I'll consider 
putting that in the next version.


More information about the Digitalmars-d-announce mailing list