@@ -734,7 +734,7 @@ const make_sh_err_com = (name, mess, com_env)=>{//«
734734} ; //»
735735
736736const get_options = ( args , com , opts = { } ) => { //«
737- const getlong = opt => {
737+ const getlong = opt => { //«
738738 let re = new RegExp ( "^" + opt ) ;
739739 let numhits = 0 ;
740740 let okkey ;
@@ -747,15 +747,16 @@ const get_options = (args, com, opts={}) => {//«
747747 if ( ! numhits ) {
748748 err . push ( `invalid option: '${ opt } '` ) ;
749749 return null ;
750- } else if ( numhits == 1 ) return okkey ;
750+ }
751+ else if ( numhits == 1 ) return okkey ;
751752 else {
752- err . push ( `option: '${ opt } ' has multiple hits` ) ;
753+ err . push ( `option '${ opt } ' has multiple hits` ) ;
753754 return null ;
754755 }
755- } ;
756+ } ; //»
756757 let err = [ ] ;
757- let sopts = opts . SHORT || opts . s ;
758- let lopts = opts . LONG || opts . l ;
758+ let sopts = opts . short || opts . SHORT || opts . s ;
759+ let lopts = opts . long || opts . LONG || opts . l ;
759760 let getall = opts . ALL ;
760761// let getall = true;
761762 let obj = { } ;
@@ -777,7 +778,8 @@ const get_options = (args, com, opts={}) => {//«
777778 args . splice ( i , 1 ) ;
778779 return [ obj , err ] ;
779780 }
780- else if ( marr = args [ i ] . match ( / ^ - ( [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 ] + ) $ / ) ) {
781+ //Short opts
782+ else if ( marr = args [ i ] . match ( / ^ - ( [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 ] + ) $ / ) ) { //«
781783 let arr = marr [ 1 ] . split ( "" ) ;
782784 for ( let j = 0 ; j < arr . length ; j ++ ) {
783785 ch = arr [ j ] ;
@@ -791,8 +793,8 @@ const get_options = (args, com, opts={}) => {//«
791793 else err . push ( `option: '${ ch } ' has an invalid option definition: ${ sopts [ ch ] } ` ) ;
792794 }
793795 args . splice ( i , 1 ) ;
794- }
795- else if ( marr = args [ i ] . match ( / ^ - ( [ a - z A - Z 0 - 9 ] ) $ / ) ) {
796+ } //»
797+ else if ( marr = args [ i ] . match ( / ^ - ( [ a - z A - Z 0 - 9 ] ) $ / ) ) { //«
796798 ch = marr [ 1 ] ;
797799 if ( getall ) {
798800 if ( ! args [ i + 1 ] ) err . push ( `option: '${ ch } ' requires an arg` ) ;
@@ -802,47 +804,69 @@ const get_options = (args, com, opts={}) => {//«
802804 else if ( ! sopts [ ch ] ) {
803805 err . push ( `invalid option: '${ ch } '` ) ;
804806 args . splice ( i , 1 ) ;
805- } else if ( sopts [ ch ] === 1 ) {
807+ }
808+ else if ( sopts [ ch ] === 1 ) {
806809 obj [ ch ] = true ;
807810 args . splice ( i , 1 ) ;
808- } else if ( sopts [ ch ] === 2 ) {
809- err . push ( `option: '${ ch } ' is an optional arg` ) ;
811+ }
812+ else if ( sopts [ ch ] === 2 ) {
813+ // err.push(`option: '${ch}' is an optional arg`);
810814 args . splice ( i , 1 ) ;
811- } else if ( sopts [ ch ] === 3 ) {
815+ if ( args [ i ] && ! args [ i ] . match ( / ^ - / ) ) {
816+ obj [ ch ] = args [ i ] ;
817+ }
818+ else obj [ ch ] = true ;
819+ }
820+ else if ( sopts [ ch ] === 3 ) {
812821 if ( ! args [ i + 1 ] ) err . push ( `option: '${ ch } ' requires an arg` ) ;
813822 obj [ ch ] = args [ i + 1 ] ;
814823 args . splice ( i , 2 ) ;
815- } else {
824+ }
825+ else {
816826 err . push ( `option: '${ ch } ' has an invalid option definition: ${ sopts [ ch ] } ` ) ;
817827 args . splice ( i , 1 ) ;
818828 }
819- } else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) = ( .+ ) $ / ) ) {
820- if ( getall || ( ret = getlong ( marr [ 1 ] ) ) ) {
821- if ( getall ) ret = marr [ 1 ] ;
829+ } //»
830+
831+ //Long opts
832+ else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) = ( .+ ) $ / ) ) { //«
833+ let lopt = marr [ 1 ] ;
834+ if ( lopts [ lopt ] === 1 ) {
835+ err . push ( `option '${ lopt } ' requires no arg` ) ;
836+ }
837+ // if (getall || (ret = getlong(marr[1]))) {
838+ else {
839+ if ( getall || ( ret = getlong ( lopt ) ) ) {
840+ if ( getall ) ret = lopt ;
822841 obj [ ret ] = marr [ 2 ] ;
823842 }
824- args . splice ( i , 1 ) ;
825- } else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) = $ / ) ) {
843+ }
844+ args . splice ( i , 1 ) ;
845+ } //»
846+ else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) = $ / ) ) { //«
826847 if ( getall || ( ret = getlong ( marr [ 1 ] ) ) ) {
827848 if ( getall ) ret = marr [ 1 ] ;
828849 obj [ ret ] = args [ i + 1 ] ;
829850 if ( args [ i + 1 ] ) args . splice ( i + 1 , 2 ) ;
830851 else args . splice ( i , 1 ) ;
831- } else args . splice ( i , 1 ) ;
832- } else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) $ / ) ) {
852+ }
853+ else args . splice ( i , 1 ) ;
854+ } //»
855+ else if ( marr = args [ i ] . match ( / ^ - - ( [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) $ / ) ) { //«
833856 if ( getall || ( ret = getlong ( marr [ 1 ] ) ) ) {
834857 if ( getall ) ret = marr [ 1 ] ;
835858 if ( getall || ( lopts [ marr [ 1 ] ] === 1 || lopts [ marr [ 1 ] ] === 2 ) ) obj [ ret ] = true ;
836859 else if ( lopts [ marr [ 1 ] ] === 3 ) err . push ( `long option: '${ marr [ 1 ] } ' requires an arg` ) ;
837860 else if ( lopts [ marr [ 1 ] ] ) err . push ( `long option: '${ marr [ 1 ] } ' has an invalid option definition: ${ lopts [ marr [ 1 ] ] } ` ) ;
838861 else if ( ! lopts [ marr [ 1 ] ] ) err . push ( `invalid long option: '${ marr [ 1 ] } ` ) ;
839862 args . splice ( i , 1 ) ;
840- } else args . splice ( i , 1 ) ;
841- }
842- else if ( marr = args [ i ] . match ( / ^ ( - - - + [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) $ / ) ) {
863+ }
864+ else args . splice ( i , 1 ) ;
865+ } //»
866+ else if ( marr = args [ i ] . match ( / ^ ( - - - + [ a - z A - Z 0 - 9 ] [ - a - z A - Z 0 - 9 ] + ) $ / ) ) { //«
843867 err . push ( `invalid option: '${ marr [ 1 ] } '` ) ;
844868 args . splice ( i , 1 ) ;
845- }
869+ } //»
846870 else i ++ ;
847871 }
848872 return [ obj , err ] ;
@@ -922,10 +946,7 @@ cwarn(`The option ${opt} already exists!`);
922946 sh_opts [ opt ] = opts [ opt ] ;
923947 }
924948 NS . coms [ libname ] = { coms, opts} ;
925-
926949 return ok_coms . length ;
927- // NS.libs[libname] = {coms, opts};
928-
929950} //»
930951const do_imports = async ( arr , err_cb ) => { //«
931952 if ( ! err_cb ) err_cb = ( ) => { } ;
@@ -994,12 +1015,13 @@ const delete_mods=(arr)=>{//«
9941015 if ( scr ) {
9951016 scr . _del ( ) ;
9961017 }
997- else {
998- //cwarn(`The module ${m} was not loaded!`);
999- continue ;
1000- }
10011018 delete NS . mods [ m ] ;
1002- //log(`Deleted module: ${m}`);
1019+ let coms = this . allLibs [ m ] ;
1020+ for ( let com of coms ) {
1021+ delete globals . shell_commands [ com ] ;
1022+ }
1023+ delete this . allLibs [ m ] ;
1024+
10031025 }
10041026} //»
10051027this . util = {
@@ -2044,7 +2066,7 @@ run(){
20442066/*
20452067continue's and break's *ALWAYS* break the "circuitry" of the logic lists.
20462068*/
2047-
2069+ /*Now in Grok.js
20482070const com_devtest = class extends Com{//«
20492071init(){
20502072}
@@ -2073,7 +2095,47 @@ log(resps);
20732095this.ok();
20742096}
20752097}//»
2098+ */
2099+
2100+ const com_devtest = class extends Com { //«
2101+
2102+ static getOpts ( ) {
2103+ //Option values
2104+ //1: no arguments
2105+ //2: optional arguments
2106+ //3: required argument
2107+ return {
2108+ "long" : { "longa" : 1 , "longb" : 2 , "longc" : 3 } ,
2109+ "short" : { "a" : 1 , "b" : 2 , "c" : 3 }
2110+ }
2111+ }
2112+ async init ( ) {
2113+ //Initialiation: option validation or resource loading may go here
2114+ }
2115+ async run ( ) {
2116+ const { args, opts, term} = this ;
2117+
2118+ // args: e.g. [ "arg1", "arg2", ... , "argn" ]
2119+ // opts: e.g. {a: true, longb: "hello world", c: 42}
2120+ // term: a handle to the terminal object with many members
2121+
2122+ this . wrn ( "Warnings are printed to the terminal in yellow" ) ;
2123+ this . inf ( "Info messages are printed in blue" ) ;
2124+ this . err ( "Errors are printed in red" ) ;
2125+ this . suc ( "This is printed in green" ) ;
2126+
2127+ this . out ( "This gets sent to pipes, command substitutions or stdout" ) ;
20762128
2129+ // The following methods end the command by internally resolving a promise
2130+ // this.end(123);//Allows for arbitrary error codes to be returned
2131+
2132+ // The following are convenience functions which can be called with an optional string to be printed to the terminal
2133+ this . ok ( ) ; //This returns a success code and any string arg printed to the terminal in green
2134+ // this.no();//This returns the standard error code (1) and any string is printed in red
2135+ // this.nok();//This calls this.ok() or this.no() depending on whether this.numErrors > 0
2136+
2137+ }
2138+ } //»
20772139const com_wat2wasm = class extends Com { //«
20782140
20792141async init ( ) { //«
@@ -2458,19 +2520,10 @@ const com_echo = class extends Com{//«
24582520 return { s : { n : 1 } } ;
24592521 }
24602522 async run ( ) {
2461-
2462- // this.out(this.args.join(" "));
24632523 let nl = this . opts . n ? "" :"\n" ;
2464- // let str = new String(this.args.join(" ")+"\n");
24652524 let str = new String ( this . args . join ( " " ) + nl ) ;
2466- //log(str.length);
24672525 str . noChomp = true ;
24682526 this . out ( str ) ;
2469-
2470- //XQMNHI
2471- //For some reason, this sleep is *required* in order to make this behave correctly:
2472- // $ echo HELLO | while read LINE; do echo $LINE; done
2473- // await sleep(0);
24742527 this . ok ( ) ;
24752528 }
24762529} //»
@@ -2708,12 +2761,12 @@ const com_stringify = class extends Com{/*«*/
27082761 }
27092762 }
27102763} /*»*/
2711- const com_clear = class extends Com { /*«*/
2764+ const com_clear = class extends Com { //«
27122765 run ( ) {
27132766 this . term . clear ( ) ;
27142767 this . ok ( ) ;
27152768 }
2716- } /*»*/
2769+ } //»
27172770const com_colon = class extends Com { //«
27182771 run ( ) {
27192772 this . ok ( ) ;
0 commit comments