-
Notifications
You must be signed in to change notification settings - Fork 1
/
Options.cs
113 lines (91 loc) · 3.02 KB
/
Options.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using CommandLine;
using CommandLine.Text;
using PrimoSoftware.AVBlocks;
namespace CliSample
{
class Options
{
// demux_mp4_file command line options
[Option('i', "input", HelpText = "Input mp4 file")]
public string InputFile { get; set; }
[Option('o', "output", HelpText = "Output mp4 filename (without extension)")]
public string OutputFile { get; set; }
// The program parses the command line options and sets these properties
public bool Error { get; private set; }
void ResetOptions()
{
InputFile = null;
OutputFile = null;
Error = false;
}
void SetDefaultOptions()
{
string exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
InputFile = Path.Combine(exeDir, "../../assets/mov/big_buck_bunny_trailer.mp4");
OutputFile = Path.Combine(exeDir, "../../output/demux_mp4_file/big_buck_bunny_trailer");
Console.WriteLine("Using default options: ");
Console.Write(" --input " + InputFile);
Console.Write(" --output " + OutputFile);
Console.WriteLine();
}
void SetOptions(Options other)
{
InputFile = other.InputFile;
OutputFile = other.OutputFile;
}
bool Validate()
{
bool res = true;
Console.Write("Input file: ");
if (InputFile == null)
{
Console.WriteLine("[not set]");
res = false;
}
else
{
Console.WriteLine(InputFile);
}
Console.Write("Output file: ");
if (OutputFile == null)
{
Console.WriteLine("[not set]");
res = false;
}
else
{
Console.WriteLine(OutputFile);
}
return res;
}
public bool Prepare(string[] args)
{
ResetOptions();
if (args.Length == 0)
{
SetDefaultOptions();
return true;
}
bool parseError = false;
var parserResult = Parser.Default.ParseArguments<Options>(args).WithNotParsed(_ => { parseError = true; });
if (parseError)
{
Error = true;
return false;
}
SetOptions(parserResult.Value);
if (!Validate())
{
var helpText = HelpText.AutoBuild(parserResult, h => h, e => e);
Console.WriteLine($"Usage:\n{helpText}");
Error = true;
return false;
}
return true;
}
}
}