Jump to content




[solved - properly this time] Processing Command Line Arguments


  • You cannot reply to this topic
14 replies to this topic

#1 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 22 November 2015 - 08:26 PM

Maybe not so solved...

http://www.computerc...post__p__239950


old post

Edited by Lupus590, 09 December 2015 - 03:07 PM.


#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 22 November 2015 - 09:45 PM

If I could request a little clarification - what problem are you having interpreting the command line arguments? Are some being skipped? None working?

I noticed a couple of things you may want to take another look at:

- Not sure if you care about this but, unless I'm mistaken, your 'lower case everything' loop will crash if one of the arguments provided is a number

- This may be part of your problem ... regarding your use of i = i + 1 in your 'process arg' loop - I'm not exactly sure what you're trying to do, but based on what you've posted, I'm guessing that you're trying to increment the value of 'i' used by the for loop. I may be wrong about this (too), but I don't think that actually modifies the value of i that is defined by the loop. I tried something similar in the past and eventually came to the conclusion that the variable used for the loop can't be 'adjusted' except by the loop itself. Unless I'm mistaken, you're actually creating another variable named i, within the loop, separate from the variable that is holding the current value for the loop. In your particular case, I think this would work...
for i = 1, #arg, 2 do
since you're parsing every other variable and using arg[i+1] to get the ones in between.

Edited by Dog, 22 November 2015 - 09:50 PM.


#3 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 22 November 2015 - 10:12 PM

your code snippet will not work at -h is a single argument

as for the actual problem, my unprocessed arg error goes off (complaining about the second arg) when I pass these as arguments
 quadPipe -t true 
to me that means that
 i = i+1 
doesn't work

the lowercase everything loop checks if the arg is a string before lowering, not tried it yet on numbers though, I can always add an escape condition

while loop with my own loop counter will have to do then, as messy as that will end up being


To clarify I'm replacing the problem for loop with a while loop, it may be a bit more messy but it should work.

Edited by Lupus590, 22 November 2015 - 10:34 PM.


#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 22 November 2015 - 10:32 PM

View PostLupus590, on 22 November 2015 - 10:12 PM, said:

your code snippet will not work at -h is a single argument
...
the lowercase everything loop checks if the arg is a string before lowering, not tried it yet on numbers though
Uggg - I missed the -h - apologies for a useless suggestion.

I may be incorrect (again), but I believe your lowercase everything loop isn't actually checking if arg[i] is a string, it's just applying tostring to arg[i] without capturing the output. You can successfully tostring(nil) as far as I know - which would pass your if tostring(arg[i]) check if I'm not mistaken. To check if it's a string, I think you probably want/need to use something like this...
if type(arg[i]) == "string" then

Edited by Dog, 22 November 2015 - 10:34 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 November 2015 - 10:36 PM

Since it's a command line argument, it will always be a string. So checking whether or not you have a string is redundant.

Why not use patterns and string manipulation to make life easy?

local args = {...}

for a, b in table.concat( args, " " ):gmatch( "%-(%a)%s(%a+)" ) do
  ...
end

In your example
quadPipe -t true

This would make a equal to "t", and b equal to "true".

Edited by KingofGamesYami, 22 November 2015 - 10:39 PM.


#6 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 22 November 2015 - 10:40 PM

I've replaced it with a while loop now, I'll check if that fixed it

#7 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 22 November 2015 - 10:42 PM

View PostKingofGamesYami, on 22 November 2015 - 10:36 PM, said:

Since it's a command line argument, it will always be a string. So checking whether or not you have a string is redundant.
...
That makes perfect sense, thank you.

Apologies for the bad advice, Lupus590 :/

Edited by Dog, 22 November 2015 - 10:42 PM.


#8 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 22 November 2015 - 10:49 PM

Well, it's stopped crashing... which means that the problem is harder to trace.

I'll bash my head on the keyboard a bit, and maybe sleep on it.

The new bug is that any arg is valid now and they don't change the variables/trigger the if statements (but do the while loop).

new code (under new paste)

Edited by Lupus590, 22 November 2015 - 10:51 PM.


#9 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 22 November 2015 - 11:10 PM

I did notice a couple of things:

On line 125 you nyError instead of myError.

Second thing - if you only pass a single argument (e.g. -h), this
while i < #arg do
will skip the loop if #arg == 1 since 1 isn't less than 1. I think you want...
while i <= #arg do

Edited by Dog, 22 November 2015 - 11:14 PM.


#10 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 November 2015 - 12:33 AM

You could also use my (old) code I made exactly for this Kind of work.

http://pastebin.com/zb5JMzbb

local cli = dofile "path to cli"
local args = cli.new{...}
args:setDefault("someValue",true)
args:parse()

--now handle the data
if not args:getBoolean "someValue" then 
  --do stuff
end


#11 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 23 November 2015 - 01:00 PM

View PostDog, on 22 November 2015 - 11:10 PM, said:

I did notice a couple of things:

On line 125 you nyError instead of myError.

Second thing - if you only pass a single argument (e.g. -h), this
while i < #arg do
will skip the loop if #arg == 1 since 1 isn't less than 1. I think you want...
while i <= #arg do

lets hope that those are the last of the bugs.

View PostH4X0RZ, on 23 November 2015 - 12:33 AM, said:

You could also use my (old) code I made exactly for this Kind of work.

http://pastebin.com/zb5JMzbb

local cli = dofile "path to cli"
local args = cli.new{...}
args:setDefault("someValue",true)
args:parse()

--now handle the data
if not args:getBoolean "someValue" then
  --do stuff
end

I do remember finding your post for that, but I couldn't find it again when I wanted to use it (have now, got it bookmarked in a more logical place now)
I've gotten this far without it, so I'd like to continue. It's part of the fun.

#12 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 23 November 2015 - 01:24 PM

I think that everything is working now (except the file part, but that's because I haven't written that yet.)

Thanks for all the help guys!

Fixed version of the code (file loading still WIP)

Edited by Lupus590, 23 November 2015 - 01:26 PM.


#13 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 09 December 2015 - 01:10 PM

You may have noticed a lack of an update on the quadPipe thread, I've had another bug and (until recently) not had the time/energy to look into it.

the test file is this (and is named test)
{"up","down"}

the current code: http://pastebin.com/mkN2XJRJ

the error: quadpipe:63: attempt to index ? (a nil value)

what I typed to get the error: quadpipe -f test

Edited by Lupus590, 09 December 2015 - 01:12 PM.


#14 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 09 December 2015 - 01:19 PM

You appear to be using "args" instead of "arg" in few lines.

#15 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 09 December 2015 - 02:36 PM

that would do it

all bugs fixed, time for cleanup and more testing

Edited by Lupus590, 09 December 2015 - 03:06 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users