Skip to content

Commit 8fe1e00

Browse files
committed
Add --args option to CashScript CLI
- When using --args you can pass in any compiler arguments that are prepended to the generated bytecode
1 parent 7b564a4 commit 8fe1e00

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

packages/cashc/src/cashc-cli.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { CashCompiler, Artifacts, version } from '.';
77
import { countOpcodes, Data, calculateBytesize } from './util';
88

99
const { argv } = yargs
10+
.parserConfiguration({ 'parse-numbers': false })
1011
.usage('Usage: $0 [options] [source_file]')
1112
.option('output', {
1213
alias: 'o',
@@ -23,6 +24,14 @@ const { argv } = yargs
2324
describe: 'Compile the contract to ASM format rather than a full artifact',
2425
type: 'boolean',
2526
})
27+
.option('args', {
28+
alias: 'a',
29+
describe: 'List of constructor arguments to pass into the contract. '
30+
+ 'Can only be used in combination with either the --hex or --asm flags. '
31+
+ 'When compiling to a JSON artifact, contract instantiation should be done through the CashScript SDK. '
32+
+ 'Note that NO type checking is performed by the cashc CLI, so it is safer to use the CashScript SDK.',
33+
type: 'array',
34+
})
2635
.showHelpOnFail(true)
2736
.help()
2837
.version(version);
@@ -32,7 +41,8 @@ run();
3241
function run(): void {
3342
ensure(argv._.length === 1, 'Please provide exactly one source file');
3443
ensure(!(argv.asm && argv.hex), 'Flags --asm and --hex can not be used together');
35-
ensure(!((argv.asm || argv.hex) && argv.output), 'Flags --asm or --hex can not be used with --output');
44+
ensure(!(argv.asm || argv.hex) || !argv.output, 'Flags --asm or --hex can not be used with --output');
45+
ensure(argv.asm || argv.hex || !argv.args, '--args can only be used with --asm or --hex');
3646

3747
const sourceFile = path.resolve(argv._[0]);
3848
const outputFile = argv.output && argv.output !== '-' && path.resolve(argv.output);
@@ -41,6 +51,24 @@ function run(): void {
4151
try {
4252
const artifact = CashCompiler.compileFile(sourceFile);
4353
const script = Data.asmToScript(artifact.bytecode);
54+
55+
if (argv.args) {
56+
argv.args.forEach((arg) => {
57+
if (typeof arg !== 'string') return; // Yargs parses everything as string
58+
if (arg === 'true') {
59+
script.unshift(Data.encodeBool(true));
60+
} else if (arg === 'false') {
61+
script.unshift(Data.encodeBool(false));
62+
} else if (arg.startsWith('0x')) {
63+
script.unshift(Buffer.from(arg.substring(2), 'hex'));
64+
} else if (!Number.isNaN(Number(arg))) {
65+
script.unshift(Data.encodeInt(Number(arg)));
66+
} else {
67+
script.unshift(Data.encodeString(arg));
68+
}
69+
});
70+
}
71+
4472
const opcount = countOpcodes(script);
4573
const bytesize = calculateBytesize(script);
4674

@@ -52,7 +80,7 @@ function run(): void {
5280
}
5381

5482
if (argv.asm) {
55-
console.log(artifact.bytecode);
83+
console.log(Data.scriptToAsm(script));
5684
return;
5785
}
5886

@@ -81,5 +109,7 @@ function ensure(condition: boolean, msg: string, code?: number): void {
81109

82110
function abort(msg: string, code: number = 1): void {
83111
console.error(msg);
112+
console.error();
113+
yargs.showHelp();
84114
process.exit(code);
85115
}

0 commit comments

Comments
 (0)