Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3dbbd46

Browse files
committed
fix up arg validation
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent8af064c commit3dbbd46

File tree

3 files changed

+98
-23
lines changed

3 files changed

+98
-23
lines changed

‎src/utils/args.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
1-
typeArgValueParams={name:string;alias?:string;param?:boolean};
1+
typeArgValueParams={
2+
name:string;
3+
alias?:string;
4+
type?:"string"|"bool"|"number";
5+
};
26

3-
constcheckValue=(
7+
functioncheckValue<T>(
48
args:string[],
59
string:string,
6-
options:ArgValueParams
7-
)=>{
10+
isBool:boolean
11+
):string|null{
812
constnameIndex=args.indexOf(string);
9-
if(nameIndex>-1){
10-
if(options.param){
11-
constvalue=args[nameIndex+1];
12-
if(!value){
13-
thrownewError(`Argument${string} is missing a parameter value`);
13+
if(nameIndex>=0){
14+
constnextArg=args[nameIndex+1];
15+
16+
if(nextArg!==undefined){
17+
constnextIsCommand=!!nextArg.match(/^\-/);
18+
if(nextIsCommand){
19+
returnisBool ?"true" :null;
1420
}
15-
returnvalue;
21+
returnnextArg;
22+
}else{
23+
// no secondary set value
24+
returnisBool ?"true" :null;
1625
}
1726
}
1827
returnnull;
19-
};
28+
}
2029

21-
exportfunctiongetArg(args:string[],options:ArgValueParams):string|null{
22-
letvalue:null|string=null;
30+
exportfunctiongetArg<T>(
31+
args:string[],
32+
options:ArgValueParams
33+
):string|boolean|number|null{
34+
letstringValue:null|string=null;
35+
constisBool=options.type==="bool";
2336

24-
constaliasString=`-${options.alias}`;
25-
value=checkValue(args,aliasString,options);
26-
if(!value){
37+
if(options.alias){
38+
constaliasString=`-${options.alias}`;
39+
stringValue=checkValue(args,aliasString,isBool);
40+
}
41+
if(!stringValue){
2742
constnameString=`--${options.name}`;
28-
value=checkValue(args,nameString,options);
43+
stringValue=checkValue(args,nameString,isBool);
44+
}
45+
46+
if(stringValue===null){
47+
returnnull;
2948
}
3049

31-
returnvalue;
50+
if(!options.type){
51+
options.type="string";
52+
}
53+
54+
// coerce type
55+
switch(options.type){
56+
case"bool":
57+
return(stringValue||"").toLowerCase()!=="false";
58+
case"number":
59+
returnNumber(stringValue);
60+
case"string":
61+
default:
62+
returnstringValue;
63+
}
3264
}

‎src/validate.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ import {
1010
}from"./utils/exec";
1111
import{getCommits,CommitLogObject}from"./utils/commits";
1212

13+
interfaceOptions{
14+
yaml:string;
15+
clean:boolean;
16+
}
17+
1318
asyncfunctionvalidate(args:string[]){
1419
// dir - default .
1520
constdir=!args.length||args[0].match(/^-/) ?"." :args[0];
1621
constlocalDir=path.join(process.cwd(),dir);
1722

1823
// -y --yaml - default coderoad-config.yml
19-
constoptions={
20-
yaml:getArg(args,{name:"yaml",alias:"y"})||"coderoad.yaml",
24+
constoptions:Options={
25+
//@ts-ignore
26+
yaml:
27+
getArg(args,{name:"yaml",alias:"y",type:"string"})||
28+
"coderoad.yaml",
29+
//@ts-ignore
30+
clean:getArg(args,{name:"clean",alias:"c",type:"bool"}),
2131
};
2232

23-
const_yaml=awaitfs.readFile(path.join(localDir,options.yaml),"utf8");
33+
const_yaml:string=awaitfs.readFile(
34+
path.join(localDir,options.yaml),
35+
"utf8"
36+
);
2437

2538
// parse yaml config
2639
letskeleton;
@@ -158,7 +171,10 @@ async function validate(args: string[]) {
158171
console.error(e.message);
159172
}finally{
160173
// cleanup
161-
awaitfs.emptyDir(tmpDir);
174+
console.log("options.clean",options.clean);
175+
if(options.clean){
176+
awaitfs.emptyDir(tmpDir);
177+
}
162178
}
163179
}
164180

‎tests/args.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ describe("args", () => {
2121
constresult=getArg(args,{name:"name",alias:"n"});
2222
expect(result).toBe("value");
2323
});
24+
it("should convert bool string to true",()=>{
25+
constargs=["--someBool","true"];
26+
constresult=getArg(args,{
27+
name:"someBool",
28+
alias:"sb",
29+
type:"bool",
30+
});
31+
expect(result).toBe(true);
32+
});
33+
it("should convert bool string to false",()=>{
34+
constargs=["--someBool","false"];
35+
constresult=getArg(args,{
36+
name:"someBool",
37+
alias:"sb",
38+
type:"bool",
39+
});
40+
expect(result).toBe(false);
41+
});
2442
it("should default value to true if no next value",()=>{
2543
constargs=["--someBool"];
2644
constresult=getArg(args,{
@@ -30,7 +48,7 @@ describe("args", () => {
3048
});
3149
expect(result).toBe(true);
3250
});
33-
it("should default value to true if next value isparam",()=>{
51+
it("should default value to true if next value is--name",()=>{
3452
constargs=["--someBool","--someOtherBool"];
3553
constresult=getArg(args,{
3654
name:"someBool",
@@ -39,4 +57,13 @@ describe("args", () => {
3957
});
4058
expect(result).toBe(true);
4159
});
60+
it("should default value to true if next value is -alias",()=>{
61+
constargs=["--someBool","-a"];
62+
constresult=getArg(args,{
63+
name:"someBool",
64+
alias:"sb",
65+
type:"bool",
66+
});
67+
expect(result).toBe(true);
68+
});
4269
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp