Torch CmdLine:text behavior is puzzling - lua

I am confused about the use of torch.CmdLine:text().
The documentation says the following:
text(string)
Logs a custom text message.
My understanding is that it adds some text message to the log file and the console. I just tried the sample code provided in the documentation page.
cmd = torch.CmdLine()
cmd:text()
cmd:text('Training a simple network')
cmd:text()
cmd:text('Options')
cmd:option('-seed',123,'initial random seed')
cmd:option('-booloption',false,'boolean option')
cmd:option('-stroption','mystring','string option')
cmd:text()
-- parse input params
params = cmd:parse(arg)
params.rundir = cmd:string('experiment', params, {dir=true})
paths.mkdir(params.rundir)
-- create log file
cmd:log(params.rundir .. '/log', params)
and I got the following output in command line and in the log file:
[program started on Sat Sep 10 14:55:30 2016]
[command line arguments]
stroption mystring
booloption false
seed 123
rundir experiment
[----------------------]
I am not seeing any output from calling text() method.
Could someone please help me in understanding what's happening here and the proper usage of text() method?

:text(string) method of CmdLine will only print string when the script is run with -help argument (and also when you decide to show help to user through its :help() method).
Example:
th MyScript.lua -help

Related

Issue in pexpect when text wraps within session

I am working on a pexpect script that is running populating an output file name and then a prompt for the file's parameters.
The program that the script runs asks for Device: then Parameters: always on the same line.... so if the file path-name that is entered for Device is long, sometimes the Parameters prompt wraps to the next line.
My code looks like..
child.expect_exact('Device:')
child.sendline('/umcfiles/ftp_dir/ftp_peoplesoft/discount/AES_DISCOUNT_15010.TXT')
child.expect_exact('Parameters:')
This times out.. and here is what is in child.before
' /umcfiles/ftp_dir/ftp_peoplesoft/discount/AES_DISCOUNT_15010.TXT Param\r\neters: "RWSN" => '
so the expect fails... (a child.expect('Parameters:') also fails)
How can I ignore the \r\n if it is there, because depending on the length of the path/filename I am using it may not be there at all, or be in a different position.
Thanks!
Actually... I found a way to calculate how much is left on the given line, and dynamically set my expect to how much of the Parameter prompt should be visible... seems to be working
#look for end of line and fix how much of 'Parameters:' we look for in pexpect
dlen = 80-len('Device: /umcfiles/ftp_dir/ftp_peoplesoft/discount/AES_DISCOUNT_15010.TXT')
pstr='Parameters:'
if dlen > len(pstr):
dlen=len(pstr)
else:
dlen=dlen-3 #remove the /r/n
child.expect(pstr[0:dlen])

groovy capture stdout last line

I have issues extracting stdout to get only the result, last(second line).
i have jenkins pipeline using groovy script which executes the following:
stage('Generate'){
stdout = bat([
returnStdout: true,
script: 'C:/folder/generate.exe --environment %ENVIRONMENT% --optionalproperties %OPTIONALPROPS%',
encoding: 'UTF-8'
]).trim();
if i pass echo stdout, to capture what this command generated, i get stdout as -
C:\folder2\folder2>C:/folder1/generate.exe --environment PROD --optionalproperties something
12345678
So my result is in new line, 12345678. I need to capture only this.
I used before to do this with:
result = stdout.readLines().drop(1).split(" ")
and i was getting just the 12345678. But it stopped working somehow.
I managed to find a workaround with this:
result = stdout.reverse().take(8).reverse()
which takes last 8 numbers and extracts them. But it's not good solution as i might have more or less amount of numbers generated, so i need a proper way to extract it.
Any advise guys what i could try else as i dont get why readLines() fails to get me any result, though the batch command didnt change?
In other words you need to get last word of output.
So you can do:
result = stdout.tokenize().last()
def last_text = .tokenize().last()
echo ${last_text} //for printing last text value

Can I read command line Parameters passed to LOLCODE

I'm using https://code.google.com/p/lolcode-dot-net/ to compile my LOLCODE.
Once I have the exe, I'd like to pass command line parameters to it. Is there some way of getting these params to the LoLcode (other than having a separate .net solution that writes it's command line parameters to a file so the Lolcode can read it.)
Here's some code
HAI
HOW DUZ I MAIN YR params
VISIBLE params
IF U SAY SO
MAIN "I'd like this from command param"
KTHXBYE
Edit: Updated the header, as David pointed out
I haven't used lolcode-dot-net, but from your code it seems you're trying to simply pass a parameter to a function.
Per the LOLCODE 1.2 language specs, this is the syntax for calling a function:
I IZ <function name> [YR <expression1> [AN YR <expression2> [AN YR <expression3> ...]]] MKAY
This works on compileonline.com:
HAI 1.2
HOW IZ I MAIN YR params
VISIBLE params
IF U SAY SO
I IZ MAIN YR "I'd like this from command param" MKAY
KTHXBYE
Output:
I'd like this from command param

Getting return status AND program output

I need to use Lua to run a binary program that may write something in its stdout and also returns a status code (also known as "exit status").
I searched the web and couldn't find something that does what I need. However I found out that in Lua:
os.execute() returns the status code
io.popen() returns a file handler that can be used to read process output
However I need both. Writing a wrapper function that runs both functions behind the scene is not an option because of process overhead and possibly changes in result on consecutive runs. I need to write a function like this:
function run(binpath)
...
return output,exitcode
end
Does anyone has an idea how this problem can be solved?
PS. the target system rung Linux.
With Lua 5.2 I can do the following and it works
-- This will open the file
local file = io.popen('dmesg')
-- This will read all of the output, as always
local output = file:read('*all')
-- This will get a table with some return stuff
-- rc[1] will be true, false or nil
-- rc[3] will be the signal
local rc = {file:close()}
I hope this helps!
I can't use Lua 5.2, I use this helper function.
function execute_command(command)
local tmpfile = '/tmp/lua_execute_tmp_file'
local exit = os.execute(command .. ' > ' .. tmpfile .. ' 2> ' .. tmpfile .. '.err')
local stdout_file = io.open(tmpfile)
local stdout = stdout_file:read("*all")
local stderr_file = io.open(tmpfile .. '.err')
local stderr = stderr_file:read("*all")
stdout_file:close()
stderr_file:close()
return exit, stdout, stderr
end
This is how I do it.
local process = io.popen('command; echo $?') -- echo return code of last run command
local lastline
for line in process:lines() do
lastline = line
end
print(lastline) -- the return code is the last line of output
If the last line has fixed length you can read it directly using file:seek("end", -offset), offset should be the length of the last line in bytes.
This functionality is provided in C by pclose.
Upon successful return, pclose() shall return the termination status
of the command language interpreter.
The interpreter returns the termination status of its child.
But Lua doesn't do this right (io.close always returns true). I haven't dug into these threads but some people are complaining about this brain damage.
http://lua-users.org/lists/lua-l/2004-05/msg00005.html
http://lua-users.org/lists/lua-l/2011-02/msg00387.html
If you're running this code on Win32 or in a POSIX environment, you could try this Lua extension: http://code.google.com/p/lua-ex-api/
Alternatively, you could write a small shell script (assuming bash or similar is available) that:
executes the correct executable, capturing the exit code into a shell variable,
prints a newline and terminal character/string onto standard out
prints the shell variables value (the exit code) onto standard out
Then, capture all the output of io.popen and parse backward.
Full disclosure: I'm not a Lua developer.
yes , your are right that os.execute() has returns and it's very simple if you understand how to run your command with and with out lua
you also may want to know how many variables it returns , and it might take a while , but i think you can try
local a, b, c, d, e=os.execute(-what ever your command is-)
for my example a is an first returned argument , b is the second returned argument , and etc.. i think i answered your question right, based off of what you are asking.

Does popen() add a line feed (\n) at the end of the result?

I've wrote a small function that returns the result of executing a command.
function axsh(cmd)
local fullCmd=cmd:lower()
local f,err=io.popen(fullCmd,"r")
if not f then
return nil,"Could not create the process '"..fullCmd.."' \nError:"..err
end
return f:read("*all")
end
s=axsh("echo hi")
--print all bytes
print(s:byte(1,s:len()))
The output always has a \n at the end no matter what is the command:
104 105 10
Edit: it happens not only for my own binary command line application but also for almost all OS commands: Windows: "dir", "ipconfig", "echo"... Linux: "ls", "pwd", "ls"...
But when I run the command separately (i.e. windows command prompt) there is no trailing line feed. I don't need it, so need to remove the last character before returning the result.
Question: does this line feed always exist in the result of popen()? I can't find any reference to this behavior in the documentation.
No. io.popen just returns whatever string the command produces. You use echo as command, which happens to put a newline after the string ( this is what makes the command prompt appear on the next line, instead of just after the output).
You can test it by using trying this:
s=axsh([[lua -e "io.write([=[hi]=])"]])
return string.byte(s,1,-1)
which does not end the output with a newline.

Resources