Home > Cheat-sheet, Myoddweb Piger > How to add command line arguments to your piger commands

How to add command line arguments to your piger commands

First the basics

am_execute( cmd, args, admin);

The first argument is the command we wish to execute, the second one is the command line argument and the last one is if we wish to run as administrator or not, (with privileged access).

How to get the arguments?

You have various commands to get the arguments entered.

You can get the ones typed by the user

-- the number of arguments
local sizeOf = am_getCommandCount();

-- argument number 0 is the action itself, (what he user typed in).
local action= am_getCommand( 0 );
...
-- argument number 1
local cmd= am_getCommand( 1 );

Or you can also get the selected folder if there is one

Remember that we could have selected more than one folder

-- do we have a folder?
local thisFolder =  am_getfolder( 0, false );

Putting it all together

If you have an app like cmder or even the default command line app you can put it all together

-- the location of the cmder.exe if it is not in the path.
local cmderPath = [[c:\path\to\your\cmder\Cmder.exe]];

-- do we have a folder?
local thisFolder =  am_getfolder( 0, false );

-- if we don't have a folder, guess what we are looking for.
if thisFolder == false  then
  local sizeOf = am_getCommandCount();
  if sizeOf == 0  then
    -- no arguments.
    am_say( "<i>Going to <b>home</b> drive</i>.", 400, 10  )
    am_execute( cmderPath, [[/START %HOMEDRIVE%\]], true );
  else
    local thisWord = am_getCommand( 1 );
    if thisWord == "home" or thisWord == "h" then
      am_say( "<i>Going to <b>home</b> drive</i>.", 400, 10  )
      am_execute( cmderPath, [[/START %HOMEDRIVE%\]], true );
    elseif thisWord == "system" or thisWord == "s" then
      am_say( "<i>Going to <b>system</b> drive</i>.", 400, 10  )
      am_execute( cmderPath, [[/START %SYSTEMDRIVE%\]], true );
    else
      am_execute( cmderPath, "", true );
    end
  end
else
  -- go to the selected folder and in case of multiple drives, change the drive letter.
  am_say( "<i>Going to <b>".. thisFolder .."</b></i>.", 400, 10  )
  am_execute( cmderPath, "/START \"" .. thisFolder .. "\"", true);
end

And save the file and call it “cmder.lua” and save it in your root command folder, (or subdirectory).

Then if the user types

  • cmder home – they will go to their home directory
  • cmder – they will go to their system drive
  • cmder (with the cursor over a folder name) – they will go to that folder.

More?

You can get more information on the piger github page

Categories: Cheat-sheet, Myoddweb Piger Tags: ,
  1. August 3rd, 2020 at 23:55 | #1

    fs91

    4

  1. No trackbacks yet.