-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandLine.ts
More file actions
83 lines (79 loc) · 2.35 KB
/
Copy pathcommandLine.ts
File metadata and controls
83 lines (79 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {writeFileSync} from 'fs';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
import {getLogger} from './src/logger.ts';
import {getRelationGpx} from './src/osm2gpx.ts';
import {getRelationKml, getRelationKmz} from './src/osm2kml.ts';
import type {RelationExporter} from './src/relation.ts';
const logger = getLogger('commandLine');
const formats = ['gpx', 'kml', 'kmz'] as const;
const exporters: Record<(typeof formats)[number], RelationExporter> = {
gpx: getRelationGpx,
kml: getRelationKml,
kmz: getRelationKmz,
};
await yargs(hideBin(process.argv))
.usage('Usage: $0 <command> [options]')
.example(
'node $0 getRelation 282071',
'Exports the Israel National Trail into a gpx file',
)
.example(
'node $0 getRelation 282071 -f kmz',
'Exports the same trail as a Google Earth kmz file',
)
.command({
command: 'getRelation <relationId>',
describe: 'Exports the relation to a gpx, kml or kmz file',
/*
* Declared here rather than in the global `.options()` block below, which
* yargs applies after the command and so leaves untyped in `argv` — the
* one option whose value is used as a lookup key needs its union type.
*/
builder: (command) =>
command
.positional('relationId', {
describe: 'Open Street Maps Relation Id to export',
type: 'number',
demandOption: true,
})
.option('format', {
alias: 'f',
choices: formats,
default: 'gpx' as const,
describe: 'The output format',
}),
async handler(argv) {
try {
const {fileName, body} = await exporters[argv.format](argv);
writeFileSync(fileName, body);
logger.info(`Done writing file "${fileName}"`);
} catch (error) {
logger.error(error);
}
},
})
.options({
s: {
alias: 'segmentLimit',
default: 9000,
describe: 'The maximum number of waypoints for each gpx track',
type: 'number',
},
m: {
alias: 'markerDiff',
default: 1000,
describe: 'The distance between markers. "0" to disable markers',
type: 'number',
},
rev: {
alias: 'reverse',
default: false,
describe: 'Reverse way sort and marker order',
type: 'boolean',
},
})
.help('h')
.alias('h', 'help')
.epilog('copyright 2015')
.parse();