Skip to content
Snippets Groups Projects
Commit 6eef99ba authored by echicken's avatar echicken :chicken:
Browse files

Allow command line arguments to override modopts values.

Parameter names are -- prefixed, with a space between name and
value. Quotes etc. around values are not handled; use escapes.
We can get fancier with this later if it's necessary.

Example:

?wttr.js --fallback_location Toronto --cache_ttl 0
parent ed238688
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
......@@ -2,21 +2,36 @@ require('http.js', 'HTTPRequest');
const locator = load({}, js.exec_dir + 'locator.js');
const xterm = load({}, js.exec_dir + 'xterm-colors.js');
function loadSettings() {
const settings = load({}, 'modopts.js', 'wttr.in') || {};
if (settings.base_url === undefined) {
settings.base_url = 'https://wttr.in/';
} else if (settings.base_url.search(/\/$/) < 0) {
settings.base_url += '/';
function parseArgs(settings, argv) {
if (argv.length < 2) return settings;
settings.fallback_ip = '';
const keys = Object.keys(settings);
for (var k = 0; k < keys.length && argv.length >= 2; k++) {
const a = argv.shift().replace(/^--/, '');
const v = argv.shift();
if (settings[a] === undefined) log(LOG_WARNING, 'wttr.in: Invalid parameter ' + a);
if (typeof settings[a] === 'number') {
const n = parseInt(v, 10);
if (isNaN(n)) throw new Error(a + ' must be a number, got ' + (typeof v) + ' v');
settings[a] = n;
} else {
settings[a] = v;
}
}
if (settings.fallback_ip === '') delete settings.fallback_ip;
return settings;
}
function loadSettings(argv) {
var settings = load({}, 'modopts.js', 'wttr.in') || {};
if (settings.base_url === undefined) settings.base_url = 'https://wttr.in/';
if (settings.units === undefined) settings.units = '';
if (settings.view === undefined) settings.view = 'AFn';
if (settings.cache_ttl === undefined) settings.cache_ttl = 3600;
if (settings.fallback_location === undefined) {
settings.fallback_location = '';
} else {
settings.fallback_location = settings.fallback_location.replace(/\s/g, '+');
}
if (settings.fallback_location === undefined) settings.fallback_location = '';
settings = parseArgs(settings, argv);
if (settings.base_url.search(/\/$/) < 0) settings.base_url += '/';
settings.fallback_location = settings.fallback_location.replace(/\s/g, '+');
return settings;
}
......@@ -63,8 +78,8 @@ function getURL(settings, addr) {
return url;
}
function getWeather() {
const settings = loadSettings();
function getWeather(argv) {
const settings = loadSettings(argv);
const addr = locator.getAddress() || settings.fallback_ip;
const url = getURL(settings, addr);
if (settings.cache_ttl > 0) {
......
......@@ -2,7 +2,7 @@ require('sbbsdefs.js', 'P_UTF8');
const wttr = load({}, js.exec_dir + 'wttr-lib.js');
function main() {
const ansi = wttr.getWeather();
const ansi = wttr.getWeather(argv);
if (js.global.console === undefined) { // jsexec
writeln(ansi);
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment