[Solved] Start up program with variables in line

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 46
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

[Solved] Start up program with variables in line

Post by AndyGable » Thursday 18th February 2021 10:23am

Hi All,

Does anyone have any guides on how I can send commands to my application when it starts up from the command line?

I mean for example

I want to run my program like this ./NPoS.gambas /Fullscreen

and when the program starts the app would read in the /Fullscreen and perform a action (maximize the form for example)

Hopefully I have explained myself here.
Last edited by AndyGable on Tuesday 23rd February 2021 5:23pm, edited 1 time in total.

User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Start up program with variables in line

Post by cogier » Thursday 18th February 2021 11:23am

The Command Line Arguments can be picked up by the Args command. If you want to do this like the pros then look at the gb.args which allows you to do this: -
Args.Begin
  sArgPattern = Args.Get("p", "pattern", "File pattern to search e.g.'*.jpg,*.jpeg'", "Pattern")
  sArgSearch = Args.Get("s", "search", "Text to search for", "Search")
  sArgFolder = Args.Get("f", "folder", "Folder to search e.g.'~/pictures'", "Folder")
  bArgCase = Args.Has("i", "ignorecase", "Ignore the case when searching")
  bArgRecursive = Args.Has("R", "recusive", "Search in the sub folders of 'Folder'")
  Args.End
You can then type on the Comman Line ./MyProg.gambas --help to get this: -

Code: Select all

Usage: InFile <options> <arguments>

Options:
 -p --pattern <Pattern>                 File pattern to search e.g.'*.jpg,*.jpeg'
 -s --search <Search>                   Text to search for
 -f --folder <Folder>                   Folder to search e.g.'~/pictures'
 -i --ignorecase <IgnoreCase>           Ignore the case when searching
 -R --recusive <Recursive>              Search in the sub folders of 'Folder'
 -V --version                           Display version
 -h --help                              Display this help

User avatar
BruceSteers
Posts: 360
Joined: Thursday 23rd July 2020 5:20pm

Re: Start up program with variables in line

Post by BruceSteers » Thursday 18th February 2021 3:10pm

without adding gb.args you can just check the Args[] array.

Args[0] is always the calling application path/name "./NPoS.gambas"
Args[1] will be "/fullscreen" with your command example

If Args.Count > 1 Then

  If Lower(Args[1]) = "/fullscreen" Then Me.Fullscreen = True

Endif

the above method Cogier mentioned is better and has the advantage of automatically supplying the -h --help arg (and -V --version)

http://gambaswiki.org/wiki/comp/gb/args (normal Args functions without adding gb.args)
http://gambaswiki.org/wiki/comp/gb.args/args (gb.args functions)

Bruce
Wishing well
Bruce

If at first you don't succeed , try it differently.

AndyGable
Posts: 46
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: Start up program with variables in line

Post by AndyGable » Friday 19th February 2021 3:29pm

BruceSteers wrote:
Thursday 18th February 2021 3:10pm
without adding gb.args you can just check the Args[] array.

Args[0] is always the calling application path/name "./NPoS.gambas"
Args[1] will be "/fullscreen" with your command example

If Args.Count > 1 Then

  If Lower(Args[1]) = "/fullscreen" Then Me.Fullscreen = True

Endif

the above method Cogier mentioned is better and has the advantage of automatically supplying the -h --help arg (and -V --version)

http://gambaswiki.org/wiki/comp/gb/args (normal Args functions without adding gb.args)
http://gambaswiki.org/wiki/comp/gb.args/args (gb.args functions)

Bruce
Thanks for the information Bruce :)

Post Reply