Re: New and having problems
- Posted by mollusk May 18, 2016
- 1917 views
Hi Mollusk
It's probably easiest to answer this by copy-pasting from the depths of the manual:
----------------
Comments:
Watch out for the following common mistake, in which the intention is to output all the characters in the third parameter but which ends up by only outputing the first character ...
include std/io.e sequence name="John Smith" printf(STDOUT, "My name is %s", name)
The output of this will be My name is J because each format specifier uses exactly one item from the values parameter. In this case we have only one specifier so it uses the first item in the values parameter, which is the character 'J'. To fix this situation, you must ensure that the first item in the values parameter is the entire text string and not just a character, so you need code this instead:
include std/io.e name="John Smith" printf(STDOUT, "My name is %s", {name})
Now, the third argument of printf() is a one-element sequence containing all the text to be formatted.
Also note that if there is only one format specifier then values can simply be an atom or integer.
-------
Also, I think there's a problem with your for loop. It keeps overwriting the arguments instead of concatenating them (so the problem only shows up when you have more than one arg). For your system call you need a single sequence (a string). Initialise pkgargs to an empty string before the loop, and then concatenate it with the commands on each iteration (don't forget the space) - e.g. pkgargs = pkgargs & cmd[i] & " ", or even better, pkgargs &= cmd[i] & " ".
PeterR
This is what I was missing! I had tried to initiate pkgargs before the loop once before but I was really missing the concatenation (the most important part). Thank you for saving me many hours. The new code looks like this:
include std/io.e include std/console.e include std/os.e include std/cmdline.e global sequence cmd = command_line() global sequence pkgargs pkgargs = " " for i = 4 to length(cmd) do --start at 4 to skip the first few arguments pkgargs &= cmd[i] & " " end for switch cmd[3] do --start at 3 to capture -i case "-i" then printf(STDOUT,"%s\n",{pkgargs}) --a debug line to print pkgargs system ("sudo xbps-install " & pkgargs,2) --simply passing pkgargs to system end switch
I will probably be back for more questions though since the next task is to tie this in with my iteration over the package managers themselves and then ultimately reading a file and using it's variables to set the proper bindings.
Just to show the package iteration script I made that is going to tie into the one you helped fix, here it is.
include std/io.e include std/filesys.e global sequence pmlist = {"apt","apt-get","yum","dnf","equo","xbps-install"} global sequence pkmgr --create variable to store found package manager global sequence gogo --Variable to bind 'PkIterate' to later and use it to evaluate return global function PkIterate() for i=1 to length(pmlist) do pkmgr = locate_file(pmlist[i]) if file_exists(pkmgr) then return pkmgr else end if end for return "false" end function gogo = PkIterate() if compare(gogo,"false") != 0 then puts(1,gogo &"\n") else puts(1, "No package manager found\n") end if
I still have a long way to go and a lot to learn. Once I get everything put together the hope is to have a modular wrapper for package managers that can read bindings from multiple config files and use the commands in the config to pass onto the package manager.