-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·79 lines (66 loc) · 1.92 KB
/
cli.js
File metadata and controls
executable file
·79 lines (66 loc) · 1.92 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
#!/usr/bin/env node
/**
* TXO URI Parser CLI
*
* A command-line interface for the TXO URI parser
* Usage: txo-parser <uri> [options]
*
* Options:
* --<field> Output only the specified field (e.g., --txid, --network, --output)
* --help Show help
*/
import { parseTxoUri, isValidTxoUri } from './index.js';
const args = process.argv.slice(2);
// Help message
function showHelp () {
console.log(`
TXO URI Parser CLI
Parse TXO URIs from the command line and output JSON or specific fields.
Usage:
txo-parser <uri> [options]
Options:
--<field> Output only the specified field (e.g., --txid, --network, --output)
--help Show this help message
Examples:
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb:0"
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb:0?amount=0.75" --txid
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb:0" --network
`);
}
// Check for help
if (args.includes('--help') || args.length === 0) {
showHelp();
process.exit(0);
}
// Get the URI and options
const uri = args[0];
const options = args.slice(1);
try {
// Parse the URI
if (!isValidTxoUri(uri)) {
console.error('Error: Invalid TXO URI');
process.exit(1);
}
const parsed = parseTxoUri(uri);
// Check if a specific field was requested
if (options.length > 0) {
for (const option of options) {
if (option.startsWith('--')) {
const field = option.substring(2);
if (parsed[field] !== undefined) {
console.log(parsed[field]);
process.exit(0);
} else {
console.error(`Error: Field "${field}" not found in parsed URI`);
process.exit(1);
}
}
}
} else {
// Output the full JSON
console.log(JSON.stringify(parsed, null, 2));
}
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}