Add decode subcommand
Accepts decimal or 0x-hex GID input. gid=0 is rendered as the literal 'empty'; non-zero values print the three components in a parse-stable space-separated key=value form.
This commit is contained in:
@@ -31,6 +31,7 @@ function run(argv, { out, err }) {
|
||||
}
|
||||
const handlers = {
|
||||
encode: require('./commands/encode'),
|
||||
decode: require('./commands/decode'),
|
||||
};
|
||||
const handler = handlers[head];
|
||||
if (!handler) {
|
||||
|
||||
39
src/commands/decode.js
Normal file
39
src/commands/decode.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const { decodeGid } = require('../gid');
|
||||
|
||||
const USAGE = 'usage: sporel-mapper decode <gid>\n';
|
||||
|
||||
function parseGid(s) {
|
||||
let n;
|
||||
if (/^0x[0-9a-fA-F]+$/.test(s)) {
|
||||
n = parseInt(s, 16);
|
||||
} else if (/^\d+$/.test(s)) {
|
||||
n = parseInt(s, 10);
|
||||
} else {
|
||||
throw new RangeError(`gid '${s}' is not a decimal or 0x-hex integer`);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function run(args, { out, err }) {
|
||||
if (args.length !== 1) {
|
||||
err.write(USAGE);
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
const gid = parseGid(args[0]);
|
||||
if (gid === 0) {
|
||||
out.write('empty\n');
|
||||
return 0;
|
||||
}
|
||||
const { atlas, tile, rotation } = decodeGid(gid);
|
||||
out.write(`atlas=${atlas} tile=${tile} rotation=${rotation}\n`);
|
||||
return 0;
|
||||
} catch (e) {
|
||||
err.write(`error: ${e.message}\n`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
Reference in New Issue
Block a user