How can I pass the arguments to the lua file through the lua CLI - lua

I have a LUA CLI which takes in the lua command,
Something like this
(lua)> #
Now here inorder to execute the lua file I run the command
(lua)> # dofile("a.lua")
I want a command which will execute the file and also pass a argument to it.
Now here I want to pass a argument to the "a.lua" file which will take this argument and call one more lua file, and this 2nd lua file is called according to the argument, So, I need to parse this argument.
Please, can somebody tell me about the parsing commands that will be used in a.lua. I mean what are the functions to be used to parse it.
Please, can somebody tell me how to pass a argument to this file "a.lua".

Now here inorder to execute the lua file I run the command
This is generally not how you execute Lua files. Usually, if you have some Lua script, you execute it with this command: lua a.lua. You don't type lua and then use the interface there to execute it.
Using a proper command line to execute the script, you can pass string parameters to the file: lua a.lua someParam "Param with spaces". The a.lua script can then fetch these parameters using the standard Lua ... mechanic:
local params = {...}
params[1] -- first parameter, if any.
params[2] -- second parameter, if any.
#params -- number of parameters.
However, if you insist on trying to execute this using your method of invoking the interpreter (with lua) and typing commands into it one by one, then you can do this:
> GlobalVariable = assert(loadfile(`a.lua`))
> GlobalVariable(--[[Insert parameters here]])
However, if you don't want to do it in two steps, with the intermediate global variable, you can do it in one:
> assert(loadfile(`a.lua`))(--[[Insert parameters here]])

Related

Oneliner to load Lua script from online (Gist) and run in current context

I have a lua REPL, and would like to run a lua script file stored as plain text at HTTPS://URL. I understand os.execute() can run OS commands so we can use curl etc. to grab the script then load(). Is that something possible to do in lua REPL with a single line?
Note: If you're going to run source code directly from the web, use https at least, to avoid easy MitM attacks.
To give this question an answer, since Egor will probably not post it as such:
(loadstring or load)(io.popen("wget -qO- https://i.imgur.com/91HtaFp.gif"):read"*a")()
For why this prints Hello world:
loadstring or load is to be compatible with different Lua versions, as the functions loadstring and load were merged at some point (5.2 I believe). io.popen executes its first argument in the shell and returns a file pointer to its stdout.
The "gif" from Egor is not really a GIF (open this in your browser: view-source:https://i.imgur.com/91HtaFp.gif) but a plain text file that contains this text:
GIF89a=GIF89a
print'Hello world'
Basically a GIF starts with GIF89a and the =GIF89a afterwards is just to produce valid Lua, meaning you don't have to use imgur or gifs, you can just as well use raw gists or github.
Now, it's rather unlikely that os.execute is available in a sandbox when io.popen is not, but if it is, you can achieve a one-liner (though drastically longer) using os.execute and temporary files
Lets first write this out because in a single line it will be a bit complex:
(function(u,f)
-- get a temp file name, Windows prefixes those with a \, so remove that
f=f or os.tmpname():gsub('^\\','')
-- run curl, make it output into our temp file
os.execute(('curl -s "%s" -o "%s"'):format(u,f))
-- load/run temp file
loadfile(f)()
os.remove(f)
end)("https://i.imgur.com/91HtaFp.gif");
And you can easily condense that into a single line by removing comments, tabs and newlines:
(function(u,f)f=f or os.tmpname():gsub('^\\','')os.execute(('curl -s "%s" -o "%s"'):format(u,f))loadfile(f)()os.remove(f)end)("https://i.imgur.com/91HtaFp.gif");

How to call a function from moonscript in lua?

I've got a moon script code like this:
hello = (name) ->
print "Hello #{name}!"
And i want to use it in my lua code using moonscript.loadfile
how should i do something like that?
MoonScript code compiles to Lua, so the function you've written is actually a Lua function when it's being executed.
There are a few ways to get access to it in Lua:
Compile the file ahead of time using the moonc command line tool. This will give you a a .lua file that you can load as you would any other Lua file.
Load the file using one of the MoonScript loader function. moonscript.loadfile is a lower level function, and I don't recommend using it unless that's what you specifically need. The easiest way is to call require "moonscript" in your program, then Lua's require function is augmented to be able to load MoonScript files directly. There's more information on the Compiler API reference page.
Keep in mind that if you have function in another file, you need to export them as part of the module. You do this by having a return value for the module. The typically pattern is to return a table that contains all the function you would want to use. In MoonScript, the last line in a file is automatically converted into a return statement. Assignment is no coerced into a return though, so I recommend structuring your module like this:
hello = (name) ->
print "Hello #{name}!"
{:hello}

How to pass URL in ruby command line?

I have written a ruby program in which it takes the input as url and it displays the components in it.
I passed the URL inside the ruby file but now I want to pass it through ruby command line like ruby filename.rb url.
How to pass an argument inside the code.
The ARGV constant holds an array which contains all of the arguments passed in on the command line. So in your script, you could get the url passed in via ARGV[0].
ARGV is an array which contains all the arguments passed into your script via the command line.
ARGV.each do|arg|
puts "Argument: #{arg}"
end
Just use ARGV constant , you can puts ARGV.to_s in log to see what`s in it , to helps you understand how to use it .

How to read parameter from Paramterized Build using ruby?

I'm using Paramterized Build, and know I can get paramter by name in bash like $name.
But I want to write script with ruby, how can I get the paramter?
Thanks
you should be able to get them using env variables
http://ruby.about.com/od/rubyfeatures/a/envvar.htm
To send parameters to your ruby script, you may use the following way:
call the script and pass the parameters from the command line (for Jenkins, you may use execute shell script option)
$ irb ruby_script.rb parameter1 parameter2
and in ruby_script.rb, you can get these parameters by using builtin ruby syntax, ARGV:
parameter_one = ARGV[0] // refer to the first paramter
parameter_one = ARGV[1] // refer to the second paramter
You may refer to this issue

How to add function definition in Lua without requiring that a file be loaded?

I am using the C Fuzzy API and I want to load function module contained in a file lets say mycalculator.lua. This seem to run fine however when I later try to run another file A.lua that requires 'mycalculator' it does not work unless the mycalculator.lua file is available on the file system to reload. I am trying to just load it into the system and then have it available without having the mycalculator.lua in the file system. It there any way to have lua system keep the definition without loading it again? Basically I convert the mycalculator.lua into a string and then run it. I don't want to put mycalculator.lua file into the file system, I just want to hand it over as a string and then be able to require it in the next string I pass to the stack Thanks
There is a difference between simply executing a Lua script and loading a Lua module. If you wish to load a Lua module, then you must actually load a Lua module exactly as a script would: by calling require.
Since you appear to be new to Lua, I should probably explain this. You've probably seen code like this in Lua scripts:
require 'mycalculator'
That is not some special statement to Lua. That is a function call. It is just some syntactic sugar for:
require('mycalculator')
Functions in Lua can be called with NAME VALUE syntax instead of NAME(...) syntax, but it only allows you to send one parameter. And the parameter must be a literal (or table constructor).
In order to call the Lua require function from C, you must use the Lua stack. You must fetch the function from the global table by using lua_getfield(L, LUA_GLOBALSINDEX, "require"); Then, you push a string onto the stack containing the name of the module to load. Then, you use lua_pcall or whatever Lua function calling function to call it.

Resources