Command line arguments in GJS (1/)

Maicmarin
Maicmarin
Feb 25, 2017 · 2 min read

I have used GNOME for many, many years so, after all this years, I just can say that I really like it.

As user, my experience has been quite excelent but, as developer, I have no experience at all; so, It’s fair to say that this is my first try to learn something about GNOME developer technologies.

Handling Command Line Arguments in GJS

Being an old school boy I chose to start learning how to handle command line arguments in GJS. The idea is to make, from now on, my own scripts in GJS instead of bash.

So lets begin, first thing you should know is that command line arguments in GJS are stored in var ARGV:

#!/usr/bin/gjs// handle1.jsprint(ARGV);

running this little code snippet shows how command line arguments are stored:

$ ./handling1.js --option1 --option2 -t file--option1,--option2,-t,file

Then you just only have to parse ARGV and handle every option with code… Easy right?, well, this sounds boring and complicated. Here GNOME libraries comes into action; searching in GObject documentation I found out there is a ‘standard’ way to handle command line arguments using GApplication (part of GIO library).

According to official documentation, GApplication is the foundation of any application and provides, among others, facilites to parse command line arguments. I’s important to notice that following code is a lot more complex and require you to know many concepts about writting applications in GJS (if you know nothing about GJS programming a good place to start is GJS wiki). So here is the code:

#!/usr/bin/gjs// handling2.jsconst System = imports.system;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Handling = new Lang.Class({
Name: ‘Handling’,
Extends: Gio.Application,
_version: ‘0.1’,
_init: function() {
this.parent({ application_id: ‘handling.example.com’,
flags: Gio.ApplicationFlags.FLAGS_NONE });
this.add_main_option(‘version’,
‘v’.charCodeAt(0),
GLib.OptionFlags.NONE,
GLib.OptionArg.NONE,
“Shows program version”,
null);
GLib.set_application_name(“Handling”);
},
vfunc_activate: function() {
this.parent();
},

vfunc_startup: function() {
this.parent();
},
vfunc_handle_local_options: function(options) {
if (options.contains(‘version’)) {
print(this._version);
return 0;
}
return -1;
},
vfunc_shutdown: function() {
this.parent();
}
});
(new Handling()).run([System.programInvocationName].concat(ARGV));

Relevant parts are:

  1. Class must ‘extend’ Gio.Application (or any subclass).
  2. ApplicationFlags should be set at object initialization. Default is FLAGS_NONE.
  3. Every argument to handle is defined using method ‘add_main_option’. You can define as many options as you want using this method. In this example I decided to handle ‘program version’.
  4. The place where you ‘handle’ every argument is in vfunc_handle_local_options method. If the return value of the function is a non-negative value then the parsing of the remaining arguments stops and process exit.

Now lets test the code example:

$ ./handling2.js --version
0.1
$
$ ./handling2.js -v
0.1
$
$ ./handling2.js --help
Usage:
gjs [OPTION...]
Help Options:
-h, --help Show help options
--help-all Show all help options
--help-gapplication Show GApplication options
Application Options:
-v, --version Shows program version
$
$ ./handling2.js --force
Unknown option --force

Amazing, using GApplication not only offers a standard way to handle command line arguments, it also parses common options as ‘help’ and catches invalid arguments.

And that’s all, there are other ways you can handle command line arguments that I will show as I learn.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade