@@ -7,6 +7,7 @@ import { CashCompiler, Artifacts, version } from '.';
77import { countOpcodes , Data , calculateBytesize } from './util' ;
88
99const { 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 ) ;
3241function 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
82110function 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