[beast: 59/70] EBEAST: parse command line, handle --version, --help and pass files to App
- From: Tim Janik <timj src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [beast: 59/70] EBEAST: parse command line, handle --version, --help and pass files to App
- Date: Tue, 28 Mar 2017 23:26:40 +0000 (UTC)
commit 2e2513849e2d39fb417f9218c6c4a2de6d2b9cd4
Author: Tim Janik <timj gnu org>
Date: Wed Mar 8 15:27:34 2017 +0100
EBEAST: parse command line, handle --version, --help and pass files to App
Signed-off-by: Tim Janik <timj gnu org>
ebeast/main.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 87 insertions(+), 2 deletions(-)
---
diff --git a/ebeast/main.js b/ebeast/main.js
index a676544..2fa80ac 100644
--- a/ebeast/main.js
+++ b/ebeast/main.js
@@ -1,5 +1,89 @@
// Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
const Electron = require ('electron');
+const ElectronDefaultApp = process.defaultApp !== undefined; // indicates unpackaged electron app
+const Eapp = Electron.app;
+
+// split command line options and return one at a time
+function pop_arg (args) {
+ if (!args.length)
+ return undefined;
+ let a = args.shift();
+ if (args.__nonoption__) { // yield saved option argument
+ args.__nonoption__--;
+ return a;
+ }
+ if (args.skip_defaultapp) { // finding defaultApp has precedence
+ if (/^[^-]/.test (a)) {
+ args.skip_defaultapp = false; // consume process.defaultApp
+ return pop_arg (args);
+ }
+ }
+ if (args.seen_separator) // yield only non-options after '--'
+ return a;
+ if (/^-[^-]+/.test (a)) { // pop single letter option
+ const rest = a.slice (2);
+ if (/^=/.test (rest)) { // save single letter option argument
+ args.unshift (rest.slice (1));
+ args.__nonoption__ = 1;
+ } else if (rest)
+ args.unshift ('-' + rest); // save adjunct single letter options
+ return '-' + a[1];
+ } else if (/^--[^-]+/.test (a)) { // pop long option
+ const eq = a.indexOf ('=');
+ if (eq > 0) {
+ args.unshift (a.slice (eq + 1)); // save long option argument
+ args.__nonoption__ = 1;
+ return a.slice (0, eq);
+ }
+ return a;
+ } else if (/^--$/.test (a)) { // stop option parsing
+ args.seen_separator = true;
+ return pop_arg (args);
+ }
+ return a;
+}
+
+function print_help () {
+ const lines = [
+ `Usage: ${Eapp.getName()} [OPTIONS] [projectfiles...]`,
+ `Options:`,
+ `--help Print command line option help`,
+ `--version Print version information`,
+ ];
+ console.log (lines.join ('\n'));
+}
+
+// parse command line arguments
+const project_files = [];
+(function () {
+ let args = process.argv.slice (1);
+ args.skip_defaultapp = ElectronDefaultApp;
+ while (args.length) {
+ const arg = pop_arg (args);
+ if (args.seen_separator) {
+ project_files.push (arg);
+ continue;
+ }
+ switch (arg) {
+ case '--help':
+ print_help();
+ Eapp.exit (0);
+ break;
+ case '--version':
+ console.log (Eapp.getName() + ' ' + Eapp.getVersion());
+ Eapp.exit (0);
+ break;
+ default:
+ if (/^-/.test (arg)) {
+ console.log ('Unknown option: ' + arg);
+ print_help();
+ Eapp.exit (129);
+ }
+ project_files.push (arg);
+ break;
+ }
+ }
+}) ();
// create the main ebeast window
var win;
@@ -42,12 +126,13 @@ function create_window ()
darkTheme: true,
};
win = new Electron.BrowserWindow (options);
+ win.bse_argv = project_files;
win.once ('ready-to-show', () => { win.show(); });
win.loadURL ('file:///' + __dirname + '/index.html');
// win.webContents.openDevTools();
win.on ('closed', () => { win = null; });
}
-Electron.app.on ('ready', create_window); // create window once everything is loaded
+Eapp.on ('ready', create_window); // create window once everything is loaded
// quit when all windows are closed.
-Electron.app.on ('window-all-closed', Electron.app.quit);
+Eapp.on ('window-all-closed', Eapp.quit);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]