@@ -32,14 +32,27 @@ export default {
32
32
args : {
33
33
$ : 'simple-parser' ,
34
34
allowPositionals : true ,
35
+ tokens : true ,
35
36
options : {
37
+ dump : {
38
+ description : 'Dump a representation of the parsed script, for debugging.' ,
39
+ type : 'boolean' ,
40
+ default : false ,
41
+ } ,
36
42
expression : {
37
43
description : 'Specify an additional script to execute. May be specified multiple times.' ,
38
44
type : 'string' ,
39
45
short : 'e' ,
40
46
multiple : true ,
41
47
default : [ ] ,
42
48
} ,
49
+ file : {
50
+ description : 'Specify a script file to execute. May be specified multiple times.' ,
51
+ type : 'string' ,
52
+ short : 'f' ,
53
+ multiple : true ,
54
+ default : [ ] ,
55
+ } ,
43
56
quiet : {
44
57
description : 'Suppress default printing of selected lines.' ,
45
58
type : 'boolean' ,
@@ -50,7 +63,7 @@ export default {
50
63
} ,
51
64
execute : async ctx => {
52
65
const { out, err } = ctx . externs ;
53
- const { positionals, values } = ctx . locals ;
66
+ const { positionals, values, tokens } = ctx . locals ;
54
67
55
68
if ( positionals . length < 1 ) {
56
69
await err . write ( 'sed: No inputs given\n' ) ;
@@ -62,16 +75,34 @@ export default {
62
75
// made, if the previous addition (if any) was from a -e option, a <newline> shall be inserted before the new
63
76
// addition. The resulting script shall have the same properties as the script operand, described in the
64
77
// OPERANDS section."
65
- // TODO: -f loads scripts from a file
66
78
let scriptString = '' ;
67
- if ( values . expression . length > 0 ) {
68
- scriptString = values . expression . join ( '\n' ) ;
79
+ if ( values . expression . length + values . file . length > 0 ) {
80
+ // These have to be in order, and -e and -f could be intermixed, so iterate the tokens
81
+ for ( let token of tokens ) {
82
+ if ( token . kind !== 'option' ) continue ;
83
+ if ( token . name === 'expression' ) {
84
+ scriptString += token . value + '\n' ;
85
+ continue ;
86
+ }
87
+ if ( token . name === 'file' ) {
88
+ for await ( const line of fileLines ( ctx , token . value ) ) {
89
+ scriptString += line ;
90
+ }
91
+ continue ;
92
+ }
93
+ }
69
94
} else {
70
95
scriptString = positionals . shift ( ) ;
71
96
}
72
97
73
- const script = parseScript ( scriptString ) ;
74
- await out . write ( script . dump ( ) ) ;
75
- await script . run ( ctx ) ;
98
+ try {
99
+ const script = parseScript ( scriptString , values ) ;
100
+ if ( values . dump )
101
+ await out . write ( script . dump ( ) ) ;
102
+ await script . run ( ctx ) ;
103
+ } catch ( e ) {
104
+ console . error ( e ) ;
105
+ await err . write ( `sed: ${ e . message } \n` ) ;
106
+ }
76
107
}
77
108
} ;
0 commit comments