-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionPrinter.cs
121 lines (104 loc) · 4.09 KB
/
OptionPrinter.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
114
115
116
117
118
119
120
121
/**
* ngetopt - Unixy command-line option parser library for .NET
* Copyright (c) 2010 Bob Carroll
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ngetopt
{
/// <summary>
/// Prints command-line options to stdout.
/// </summary>
public sealed class OptionPrinter
{
private string _usage;
private OptionExtra[] _opts;
private string[] _groups;
/// <summary>
/// Initialises the option printer.
/// </summary>
/// <param name="usage">command format</param>
/// <param name="opts">an array of options</param>
public OptionPrinter(string usage, OptionExtra[] opts)
: this(usage, opts, new string[] { }) { }
/// <summary>
/// Initialises the option printer.
/// </summary>
/// <param name="usage">command format</param>
/// <param name="opts">an array of options</param>
/// <param name="groups">an array of option group names to print</param>
public OptionPrinter(string usage, OptionExtra[] opts, string[] groups)
{
_usage = usage;
_opts = opts;
_groups = groups;
this.OptionWidth = 32;
}
/// <summary>
/// Prints an option group to stdout.
/// </summary>
/// <param name="groupname">group title</param>
public void PrintGroup(string groupname)
{
OptionExtra[] opts = groupname != null ?
_opts.Where(i => i.InGroup == groupname).ToArray() :
_opts;
if (opts.Length == 0) return;
StringBuilder sbgroup = new StringBuilder();
sbgroup.AppendFormat("{0}:\n",
groupname != null ? groupname : "Options");
foreach (OptionExtra oe in opts) {
StringBuilder sbline = new StringBuilder();
sbline.AppendFormat(" -{0}", oe.Option.Val);
string arglabel = oe.ArgLabel != null ? oe.ArgLabel : "ARG";
if (oe.Option.HasArg == ArgFlags.Required)
sbline.AppendFormat(" {0}", arglabel);
else if (oe.Option.HasArg == ArgFlags.Optional)
sbline.AppendFormat(" [{0}]", arglabel);
if (oe.Option.Name.Length > 1) {
sbline.AppendFormat(", --{0}", oe.Option.Name);
if (oe.Option.HasArg == ArgFlags.Required)
sbline.AppendFormat("={0}", arglabel);
else if (oe.Option.HasArg == ArgFlags.Optional)
sbline.AppendFormat("=[{0}]", arglabel);
}
sbgroup.AppendFormat("{0}{1}\n",
sbline.ToString().PadRight(this.OptionWidth),
oe.Description);
}
Console.WriteLine(sbgroup.ToString());
}
/// <summary>
/// Prints usage instructions to stdout.
/// </summary>
public void PrintUsage()
{
Console.WriteLine("Usage: {0}\n", _usage);
if (_groups.Length > 0) {
foreach (string g in _groups)
this.PrintGroup(g);
} else
this.PrintGroup(null);
}
/// <summary>
/// Width of the options column.
/// </summary>
public int OptionWidth { get; set; }
}
}