digitalmars.D.announce - echo: "-n", the next installment
- John Colvin (3/3) Jul 25 2013 After a few weeks of not getting around to it, here's my second
- Jesse Phillips (4/7) Jul 26 2013 BTW, std.getopt is a good way to parse arguments. Not sure if it
- John Colvin (3/11) Jul 27 2013 I'm pretty sure it wouldn't work ideally for echo as the
- Joseph Rushton Wakeling (4/6) Jul 27 2013 It also has some odd little niggles -- e.g. it's not nice that
- monarch_dodra (10/22) Jul 27 2013 getopt knows how to handle ordering, it's really just a matter of
- John Colvin (9/34) Jul 27 2013 Also, this wouldn't work
- monarch_dodra (24/27) Jul 27 2013 I tried to post a comment on your blog, but I failed. Anyways, I
- monarch_dodra (33/40) Jul 27 2013 And here is the second comment I wanted to put:
- John Colvin (5/37) Jul 27 2013 thanks for the info on writef, I didn't realise it could do that.
After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html
Jul 25 2013
On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlBTW, std.getopt is a good way to parse arguments. Not sure if it is relevant to what you want to teach, but should generally be preferred over handwritten.
Jul 26 2013
On Saturday, 27 July 2013 at 01:09:03 UTC, Jesse Phillips wrote:On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:I'm pretty sure it wouldn't work ideally for echo as the behaviour depends on the order of the arguments.After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlBTW, std.getopt is a good way to parse arguments. Not sure if it is relevant to what you want to teach, but should generally be preferred over handwritten.
Jul 27 2013
On Saturday, 27 July 2013 at 12:19:44 UTC, John Colvin wrote:I'm pretty sure it wouldn't work ideally for echo as the behaviour depends on the order of the arguments.It also has some odd little niggles -- e.g. it's not nice that with a short option you can have --t 5 and --t=5 but not -t 5 or -t=5 (according to docs you can only use -t5 ).
Jul 27 2013
On Saturday, 27 July 2013 at 12:19:44 UTC, John Colvin wrote:On Saturday, 27 July 2013 at 01:09:03 UTC, Jesse Phillips wrote:getopt knows how to handle ordering, it's really just a matter of echo's argument parsing rules being different from classic getopt. For example, echo does not handle "--" argument (end of options mark), which means it is literally impossible for echo's first "string argument" to be "-n". So for example, while "echo -- -n" would print "-- -n", a getopt echo would print "-n". Arguably, this is better behavior, but if the goal is exact replication, then it's wrong :/On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:I'm pretty sure it wouldn't work ideally for echo as the behaviour depends on the order of the arguments.After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlBTW, std.getopt is a good way to parse arguments. Not sure if it is relevant to what you want to teach, but should generally be preferred over handwritten.
Jul 27 2013
On Saturday, 27 July 2013 at 12:59:06 UTC, monarch_dodra wrote:On Saturday, 27 July 2013 at 12:19:44 UTC, John Colvin wrote:Also, this wouldn't work $ echo -E fdsa -n fdsa -n $ as getopt would parse the -n as an option. Exact replication is the name of the game here, as often in the real world one has to write to a spec that is subtly different to what library writers had in mind.On Saturday, 27 July 2013 at 01:09:03 UTC, Jesse Phillips wrote:getopt knows how to handle ordering, it's really just a matter of echo's argument parsing rules being different from classic getopt. For example, echo does not handle "--" argument (end of options mark), which means it is literally impossible for echo's first "string argument" to be "-n". So for example, while "echo -- -n" would print "-- -n", a getopt echo would print "-n". Arguably, this is better behavior, but if the goal is exact replication, then it's wrong :/On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:I'm pretty sure it wouldn't work ideally for echo as the behaviour depends on the order of the arguments.After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlBTW, std.getopt is a good way to parse arguments. Not sure if it is relevant to what you want to teach, but should generally be preferred over handwritten.
Jul 27 2013
On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlI tried to post a comment on your blog, but I failed. Anyways, I wanted to post: In regards to the whole "spaces only *between*" elements issue, it is always good to know that writef knows how to print a range, and knows how to add a "separator" tokens. This make it trivailly easy to print a range of elements, adding tokens *only* between each elements. I know it's not necessarily what you want to teach, but it *is* incredibly useful. Your first example becomes the trivial: import std.stdio : writefln; void main(string[] args) { assert(args.length); args = args[1 .. $]; debug writefln("[%(%s, %)]", args); else writefln("%-(%s %)", args); } when invoked with "a b c", it prints: a b c If compiled with "-debug" to boot, you can add extra "visual" tokens to check there are no trailing symbols: ["a", "b", "c"] See? Who needs a foreach, or a loop, or functional programming ;)
Jul 27 2013
On Saturday, 27 July 2013 at 12:39:24 UTC, monarch_dodra wrote:On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin 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 :)After a few weeks of not getting around to it, here's my second post: http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.htmlI tried to post a comment on your blog, but I failed. Anyways, I wanted to post:
Jul 27 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.
Jul 27 2013