Skip to content
This repository was archived by the owner on Aug 2, 2023. It is now read-only.

Commit fec0fd8

Browse files
committed
Merge pull request #280 from dotnet/terrajobst/commandline
Command line parser
2 parents ad6a087 + dade269 commit fec0fd8

25 files changed

+4524
-0
lines changed

src/System.CommandLine/README.md

Lines changed: 493 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine", "src\System.CommandLine.csproj", "{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine.Tests", "tests\System.CommandLine.Tests.csproj", "{F48BE89B-4F3E-4AB3-B10A-D6AE47869190}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F48BE89B-4F3E-4AB3-B10A-D6AE47869190}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F48BE89B-4F3E-4AB3-B10A-D6AE47869190}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F48BE89B-4F3E-4AB3-B10A-D6AE47869190}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F48BE89B-4F3E-4AB3-B10A-D6AE47869190}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<OutputType>Library</OutputType>
8+
<RootNamespace>
9+
</RootNamespace>
10+
<AssemblyName>System.CommandLine</AssemblyName>
11+
<AssemblyVersion>4.0.0.0</AssemblyVersion>
12+
<ProjectGuid>{0A365F6D-AF33-4DD1-ABF3-BE45A5F8E642}</ProjectGuid>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<None Include="project.json" />
20+
</ItemGroup>
21+
<ItemGroup>
22+
<Compile Include="System\CommandLine\ArgumentParser.cs" />
23+
<Compile Include="System\CommandLine\InternalsVisibleTo.cs" />
24+
<Compile Include="System\CommandLine\ArgumentSyntax.cs" />
25+
<Compile Include="System\CommandLine\ArgumentSyntax_Definers.cs" />
26+
<Compile Include="System\CommandLine\Argument.cs" />
27+
<Compile Include="System\CommandLine\ArgumentList`1.cs" />
28+
<Compile Include="System\CommandLine\Argument`1.cs" />
29+
<Compile Include="System\CommandLine\ArgumentCommand.cs" />
30+
<Compile Include="System\CommandLine\ArgumentCommand`1.cs" />
31+
<Compile Include="System\CommandLine\ArgumentSyntaxException.cs" />
32+
<Compile Include="System\CommandLine\ArgumentToken.cs" />
33+
<Compile Include="System\CommandLine\ArgumentLexer.cs" />
34+
<Compile Include="System\CommandLine\HelpTextGenerator.cs" />
35+
<Compile Include="System\Strings.Designer.cs">
36+
<AutoGen>True</AutoGen>
37+
<DesignTime>True</DesignTime>
38+
<DependentUpon>Strings.resx</DependentUpon>
39+
</Compile>
40+
</ItemGroup>
41+
<ItemGroup>
42+
<EmbeddedResource Include="System\Strings.resx">
43+
<Generator>ResXFileCodeGenerator</Generator>
44+
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
45+
</EmbeddedResource>
46+
</ItemGroup>
47+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
48+
</Project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Linq;
8+
9+
namespace System.CommandLine
10+
{
11+
public abstract class Argument
12+
{
13+
internal Argument(ArgumentCommand command, IEnumerable<string> names, bool isOption)
14+
{
15+
var nameArray = names.ToArray();
16+
Command = command;
17+
Name = nameArray.First();
18+
Names = new ReadOnlyCollection<string>(nameArray);
19+
IsOption = isOption;
20+
}
21+
22+
public ArgumentCommand Command { get; private set; }
23+
24+
public string Name { get; private set; }
25+
26+
public ReadOnlyCollection<string> Names { get; private set; }
27+
28+
public string Help { get; set; }
29+
30+
public bool IsOption { get; private set; }
31+
32+
public bool IsParameter
33+
{
34+
get { return !IsOption; }
35+
}
36+
37+
public bool IsSpecified { get; private set; }
38+
39+
public bool IsHidden { get; set; }
40+
41+
public virtual bool IsList
42+
{
43+
get { return false; }
44+
}
45+
46+
public object Value
47+
{
48+
get { return GetValue(); }
49+
}
50+
51+
public object DefaultValue
52+
{
53+
get { return GetDefaultValue(); }
54+
}
55+
56+
public bool IsActive
57+
{
58+
get { return Command == null || Command.IsActive; }
59+
}
60+
61+
public abstract bool IsFlag { get; }
62+
63+
internal abstract object GetValue();
64+
65+
internal abstract object GetDefaultValue();
66+
67+
internal void MarkSpecified()
68+
{
69+
IsSpecified = true;
70+
}
71+
72+
public string GetDisplayName()
73+
{
74+
return GetDisplayName(Name);
75+
}
76+
77+
public IEnumerable<string> GetDisplayNames()
78+
{
79+
return Names.Select(GetDisplayName);
80+
}
81+
82+
private string GetDisplayName(string name)
83+
{
84+
return IsOption ? GetOptionDisplayName(name) : GetParameterDisplayName(name);
85+
}
86+
87+
private static string GetOptionDisplayName(string name)
88+
{
89+
var modifier = name.Length == 1 ? @"-" : @"--";
90+
return modifier + name;
91+
}
92+
93+
private static string GetParameterDisplayName(string name)
94+
{
95+
return @"<" + name + @">";
96+
}
97+
98+
public virtual string GetDisplayValue()
99+
{
100+
return Value == null ? string.Empty : Value.ToString();
101+
}
102+
103+
public override string ToString()
104+
{
105+
return GetDisplayName();
106+
}
107+
}
108+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace System.CommandLine
7+
{
8+
public abstract class ArgumentCommand
9+
{
10+
internal ArgumentCommand(string name)
11+
{
12+
Name = name;
13+
}
14+
15+
public string Name { get; private set; }
16+
17+
public string Help { get; set; }
18+
19+
public object Value
20+
{
21+
get { return GetValue(); }
22+
}
23+
24+
public bool IsHidden { get; set; }
25+
26+
public bool IsActive { get; private set; }
27+
28+
internal abstract object GetValue();
29+
30+
internal void MarkActive()
31+
{
32+
IsActive = true;
33+
}
34+
35+
public override string ToString()
36+
{
37+
return Name;
38+
}
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace System.CommandLine
7+
{
8+
public sealed class ArgumentCommand<T> : ArgumentCommand
9+
{
10+
internal ArgumentCommand(string name, T value)
11+
: base(name)
12+
{
13+
Value = value;
14+
}
15+
16+
public new T Value { get; private set; }
17+
18+
internal override object GetValue()
19+
{
20+
return Value;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)