Skip to content

Commit 29a24e8

Browse files
committed
Add cli args to console app
1 parent 379e3c0 commit 29a24e8

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

ValveKeyValue/ValveKeyValue.Console/Program.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
using System.Text;
22
using ValveKeyValue;
3+
using ConsoleAppFramework;
34

45
Console.OutputEncoding = Encoding.UTF8;
56

6-
if (args.Length < 1)
7+
ConsoleApp.Run(args, Execute);
8+
9+
/// <summary>
10+
/// Parse a KeyValues file and print to console.
11+
/// </summary>
12+
/// <param name="file">-f, Input file to be parsed.</param>
13+
/// <param name="escape">Whether the parser should translate escape sequences.</param>
14+
/// <param name="valve_null_bug">Whether invalid escape sequences should truncate strings rather than throwing.</param>
15+
static int Execute(
16+
string file,
17+
bool escape = false,
18+
bool valve_null_bug = false
19+
)
720
{
8-
Console.Error.WriteLine("Provide path to a keyvalues file as the first argument.");
9-
return 1;
10-
}
11-
12-
var file = args[0];
13-
14-
if (!File.Exists(file))
15-
{
16-
Console.Error.WriteLine($"File \"{file}\" does not exist.");
17-
return 1;
18-
}
21+
if (!File.Exists(file))
22+
{
23+
Console.Error.WriteLine($"File \"{file}\" does not exist.");
24+
return 1;
25+
}
1926

20-
using var stream = File.OpenRead(file);
27+
using var stream = File.OpenRead(file);
2128

22-
var options = new KVSerializerOptions
23-
{
24-
HasEscapeSequences = true,
25-
};
26-
var serializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
27-
var root = serializer.Deserialize(stream, options);
29+
var options = new KVSerializerOptions
30+
{
31+
HasEscapeSequences = escape,
32+
EnableValveNullByteBugBehavior = valve_null_bug
33+
};
34+
var serializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
35+
var root = serializer.Deserialize(stream, options);
2836

29-
RecursivePrint(root);
37+
RecursivePrint(root);
3038

31-
return 0;
39+
return 0;
40+
}
3241

3342
static void RecursivePrint(KVObject obj, int indent = 0)
3443
{

ValveKeyValue/ValveKeyValue.Console/ValveKeyValue.Console.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<RollForward>LatestMajor</RollForward>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="ConsoleAppFramework" Version="5.2.4">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
</ItemGroup>
915
<ItemGroup>
1016
<ProjectReference Include="..\ValveKeyValue\ValveKeyValue.csproj" />
1117
</ItemGroup>

0 commit comments

Comments
 (0)