├── .editorconfig ├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── .gitignore ├── CHANGELOG.md ├── CommandLine.sln ├── CommandLine.snk ├── Directory.Build.props ├── License.md ├── README.md ├── appveyor.yml ├── art ├── CommandLine.png ├── CommandLine20.png ├── CommandLineNuGet.png ├── CommandLinePreRelNuGet.png └── resharper-logo.png ├── demo ├── ReadText.Demo.VB │ ├── App.config │ ├── My Project │ │ ├── Application.Designer.vb │ │ ├── Application.myapp │ │ ├── AssemblyInfo.vb │ │ ├── Resources.Designer.vb │ │ ├── Resources.resx │ │ ├── Settings.Designer.vb │ │ ├── Settings.settings │ │ └── app.manifest │ ├── Options.vb │ ├── Program.vb │ ├── ReadText.Demo.VB.sln │ ├── ReadText.Demo.VB.vbproj │ ├── ReadText.Demo.VB.vbproj.user │ └── packages.config ├── ReadText.Demo │ ├── Options.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ReadText.Demo.csproj │ ├── ReadText.Demo.sln │ └── packages.config ├── ReadText.LocalizedDemo │ ├── .cr │ │ └── personal │ │ │ └── Navigation │ │ │ └── RecentFilesHistory.xml │ ├── LocalizableSentenceBuilder.cs │ ├── Options.cs │ ├── Program.cs │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ ├── Resources.cs.resx │ │ ├── Resources.resx │ │ └── launchSettings.json │ ├── ReadText.LocalizedDemo.csproj │ ├── ReadText.LocalizedDemo.sln │ └── packages.config └── fsharp-demo.fsx ├── docs ├── ChangeLog ├── Contributors ├── INFO ├── PublicAPI.md ├── content │ ├── index.fsx │ └── tutorial.fsx ├── files │ └── img │ │ ├── logo-template.pdn │ │ └── logo.png └── tools │ ├── FSharp.Formatting.svclog │ ├── generate.fsx │ ├── packages.config │ └── templates │ └── template.cshtml ├── src └── CommandLine │ ├── BaseAttribute.cs │ ├── CastExtensions.cs │ ├── CommandLine.csproj │ ├── Core │ ├── ArgumentsExtensions.cs │ ├── GetoptTokenizer.cs │ ├── InstanceBuilder.cs │ ├── InstanceChooser.cs │ ├── KeyValuePairHelper.cs │ ├── NameExtensions.cs │ ├── NameLookup.cs │ ├── OptionMapper.cs │ ├── OptionSpecification.cs │ ├── PartitionExtensions.cs │ ├── PreprocessorGuards.cs │ ├── ReflectionExtensions.cs │ ├── Specification.cs │ ├── SpecificationExtensions.cs │ ├── SpecificationGuards.cs │ ├── SpecificationProperty.cs │ ├── SpecificationPropertyExtensions.cs │ ├── SpecificationPropertyRules.cs │ ├── Token.cs │ ├── TokenPartitioner.cs │ ├── Tokenizer.cs │ ├── TypeConverter.cs │ ├── TypeDescriptor.cs │ ├── TypeLookup.cs │ ├── ValueMapper.cs │ ├── ValueSpecification.cs │ └── Verb.cs │ ├── Error.cs │ ├── ErrorExtensions.cs │ ├── HelpTextExtensions.cs │ ├── Infrastructure │ ├── CSharpx │ │ ├── Either.cs │ │ ├── EnumerableExtensions.cs │ │ └── Maybe.cs │ ├── EnumerableExtensions.cs │ ├── ErrorHandling.cs │ ├── ExceptionExtensions.cs │ ├── FSharpOptionHelper.cs │ ├── LocalizableAttributeProperty.cs │ ├── PopsicleSetter.cs │ ├── ReferenceEqualityComparer.cs │ ├── ReflectionHelper.cs │ ├── StringBuilderExtensions.cs │ └── StringExtensions.cs │ ├── IntrospectionExtensions.cs │ ├── NameInfo.cs │ ├── NullInstance.cs │ ├── OptionAttribute.cs │ ├── Parser.cs │ ├── ParserExtensions.cs │ ├── ParserResult.cs │ ├── ParserResultExtensions.cs │ ├── ParserResultExtensionsAsync.cs │ ├── ParserSettings.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Text │ ├── AssemblyLicenseAttribute.cs │ ├── AssemblyUsageAttribute.cs │ ├── CopyrightInfo.cs │ ├── Example.cs │ ├── HeadingInfo.cs │ ├── HelpText.cs │ ├── MultiLineTextAttribute.cs │ ├── SentenceBuilder.cs │ ├── TextWrapper.cs │ └── UsageAttribute.cs │ ├── UnParserExtensions.cs │ ├── ValueAttribute.cs │ └── VerbAttribute.cs └── tests └── CommandLine.Tests ├── CommandLine.Tests.csproj ├── CultureInfoExtensions.cs ├── Fakes ├── CustomAttribute.cs ├── Custom_Struct.cs ├── HelpTextWithLineBreaksAndSubIndentation_Options.cs ├── HelpTextWithLineBreaks_Options.cs ├── HelpTextWithMixedLineBreaks_Options.cs ├── Help_Fakes.cs ├── Hidden_Option.cs ├── IInterface_With_Two_Scalar_Options.cs ├── Immutable_Simple_Options.cs ├── Immutable_Verb_Fakes.cs ├── Mutable_Without_Empty_Constructor.cs ├── Options_HelpText_Ordering.cs ├── Options_With_Both_Min_And_Max_Set_To_Zero.cs ├── Options_With_Custom_Help_Option.cs ├── Options_With_Custom_Version_Option.cs ├── Options_With_Default_Set_To_Sequence.cs ├── Options_With_Defaults.cs ├── Options_With_Enum_Having_HelpText.cs ├── Options_With_FSharpOption.cs ├── Options_With_FileDirectoryInfo.cs ├── Options_With_FlagCounter_Switches.cs ├── Options_With_Group.cs ├── Options_With_Guid.cs ├── Options_With_HelpText_And_MetaValue.cs ├── Options_With_Interface.cs ├── Options_With_InvalidDefaults.cs ├── Options_With_Max_Set_To_Zero.cs ├── Options_With_MetaValue.cs ├── Options_With_Min_Set_To_Zero.cs ├── Options_With_Multiple_Groups.cs ├── Options_With_Named_And_Empty_Sets.cs ├── Options_With_Nullable_Enum_Having_HelpText.cs ├── Options_With_Nullables.cs ├── Options_With_Only_Explicit_Interface.cs ├── Options_With_Option_And_Value_Of_String_Type.cs ├── Options_With_Option_Sequence_And_Value_Sequence.cs ├── Options_With_Property_Throwing_Exception.cs ├── Options_With_Required_Set_To_True.cs ├── Options_With_Required_Set_To_True_For_Values.cs ├── Options_With_Required_Set_To_True_Within_Same_Set.cs ├── Options_With_Scalar_Value_And_Adjacent_SequenceString.cs ├── Options_With_Sequence.cs ├── Options_With_Sequence_And_Only_Max_Constraint.cs ├── Options_With_Sequence_And_Only_Max_Constraint_For_Value.cs ├── Options_With_Sequence_And_Only_Min_Constraint.cs ├── Options_With_Sequence_And_Only_Min_Constraint_For_Value.cs ├── Options_With_Sequence_Having_Both_Min_And_Max_Equal.cs ├── Options_With_Sequence_Having_Separator_And_Values.cs ├── Options_With_Sequence_Having_Separator_Set.cs ├── Options_With_Sequence_Without_Range_For_Value.cs ├── Options_With_SetName_That_Ends_With_Previous_SetName.cs ├── Options_With_Shuffled_Index_Values.cs ├── Options_With_Similar_Names.cs ├── Options_With_Switches.cs ├── Options_With_TimeSpan.cs ├── Options_With_Two_Option_Required_Set_To_True_And_Two_Sets.cs ├── Options_With_Two_Options_Having_Required_Set_To_True.cs ├── Options_With_Two_Sets.cs ├── Options_With_Uri_And_SimpleType.cs ├── Options_With_Value_Sequence_And_Normal_Option.cs ├── ResourceFakes.cs ├── Simple_Options.cs ├── Simple_Options_With_Double_Value.cs ├── Simple_Options_With_Enum.cs ├── Simple_Options_With_ExtraArgs.cs ├── Simple_Options_With_Multiple_OptionGroups.cs ├── Simple_Options_With_OptionGroup.cs ├── Simple_Options_With_OptionGroup_MutuallyExclusiveSet.cs ├── Simple_Options_With_OptionGroup_WithDefaultValue.cs ├── Simple_Options_With_OptionGroup_WithOptionDefaultValue.cs ├── Simple_Options_With_Required_OptionGroup.cs ├── Simple_Options_With_Values.cs └── Verb_Fakes.cs ├── StringExtensions.cs └── Unit ├── BaseAttributeTests.cs ├── Core ├── GetoptTokenizerTests.cs ├── InstanceBuilderTests.cs ├── InstanceChooserTests.cs ├── KeyValuePairHelperTests.cs ├── NameLookupTests.cs ├── OptionMapperTests.cs ├── ReflectionExtensions.cs ├── ScalarTests.cs ├── SequenceTests.cs ├── SpecificationPropertyRulesTests.cs ├── SwitchTests.cs ├── TextWrapperTests.cs ├── TokenPartitionerTests.cs ├── TokenTests.cs ├── TokenizerTests.cs └── TypeConverterTests.cs ├── GetoptParserTests.cs ├── Infrastructure └── FSharpOptionHelperTests.cs ├── Issue104Tests.cs ├── Issue389Tests.cs ├── Issue418Tests.cs ├── Issue424Tests.cs ├── Issue482Tests.cs ├── Issue543Tests.cs ├── Issue591Tests.cs ├── Issue6Tests.cs ├── Issue70Tests.cs ├── Issue776Tests.cs ├── ParserResultExtensionsTests.cs ├── ParserSettingsTests.cs ├── ParserTests.cs ├── SequenceParsingTests.cs ├── StringBuilderExtensionsTests.cs ├── Text ├── HelpTextAutoBuildFix.cs └── HelpTextTests.cs ├── UnParserExtensionsTests.cs └── VerbAttributeTests.cs /.editorconfig: -------------------------------------------------------------------------------- 1 | #top-most EditorConfig for project 2 | root = true 3 | 4 | [*] 5 | end_of_line = crlf 6 | insert_final_newline = true 7 | 8 | [*.cs] 9 | indent_style = space 10 | indent_size = 4 11 | 12 | [*.{xml,csproj,config}] 13 | indent_style = tab 14 | indent_size = 4 15 | 16 | [*.json] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [*.js] 21 | indent_style = space 22 | indent_size = 2 23 | 24 | [*.yml] 25 | indent_style = space 26 | indent_size = 2 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Either fork from this fiddle and paste link here: https://dotnetfiddle.net/mh9CjX 15 | 16 | or 17 | 18 | Steps to reproduce the behavior: 19 | 1. Go to '...' 20 | 2. Click on '....' 21 | 3. Scroll down to '....' 22 | 4. See error 23 | 24 | **Expected behavior** 25 | A clear and concise description of what you expected to happen. 26 | 27 | **Screenshots** 28 | If applicable, add screenshots to help explain your problem. 29 | 30 | **Additional context** 31 | Add any other context about the problem here. 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | #ignore build outputs 3 | [B|b]in 4 | [O|o]bj 5 | build/* 6 | 7 | #ignore managed external libs 8 | packages 9 | paket-files 10 | 11 | *.suo 12 | *.userprefs 13 | *~ 14 | \#*\# 15 | *.pidb 16 | 17 | #ignore Test results/temps 18 | *.test-cache 19 | tests/CommandLine/test-results/* 20 | tests/CommandLine.Tests/test-results/* 21 | TestResult.xml 22 | 23 | *.DS_Store 24 | *.csproj.user 25 | *.nupkg 26 | *.old 27 | StyleCop.Cache 28 | .paket/paket.exe 29 | .fake 30 | *.cache 31 | docs/output/* 32 | artifacts/* 33 | *.xproj.user 34 | *.nuget.targets 35 | *.lock.json 36 | *.nuget.props 37 | *.DotSettings.user 38 | # Visual Studio 2015 cache/options directory 39 | .vs/ 40 | # Rider 41 | .idea/ 42 | 43 | [R|r]elease/** 44 | -------------------------------------------------------------------------------- /CommandLine.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2042 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine", "src\CommandLine\CommandLine.csproj", "{E1BD3C65-49C3-49E7-BABA-C60980CB3F20}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine.Tests", "tests\CommandLine.Tests\CommandLine.Tests.csproj", "{0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1361E8B1-D0E1-493E-B8C1-7380A7B7C472}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {E1BD3C65-49C3-49E7-BABA-C60980CB3F20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {E1BD3C65-49C3-49E7-BABA-C60980CB3F20}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {E1BD3C65-49C3-49E7-BABA-C60980CB3F20}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {E1BD3C65-49C3-49E7-BABA-C60980CB3F20}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E}.Release|Any CPU.Build.0 = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(SolutionProperties) = preSolution 28 | HideSolutionNode = FALSE 29 | EndGlobalSection 30 | GlobalSection(NestedProjects) = preSolution 31 | {0A15C4D2-B3E9-43AB-8155-1B39F7AC8A5E} = {1361E8B1-D0E1-493E-B8C1-7380A7B7C472} 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {5B5A476C-82FB-49FB-B592-5224D9005186} 35 | EndGlobalSection 36 | GlobalSection(MonoDevelopProperties) = preSolution 37 | StartupItem = src\CommandLine\CommandLine.csproj 38 | EndGlobalSection 39 | EndGlobal 40 | -------------------------------------------------------------------------------- /CommandLine.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/CommandLine.snk -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | CS1591;CS0219;8002;NU5125 4 | $(MSBuildThisFileDirectory) 5 | false 6 | 7 | 8 | $(DefineConstants);NETFRAMEWORK 9 | 10 | 11 | 12 | 13 | runtime; build; native; contentfiles; analyzers 14 | all 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2005 - 2015 Giacomo Stelluti Scala & Contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | #version should be only changed with RELEASE eminent, see RELEASE.md 2 | 3 | version: 2.9.1-ci-{build} 4 | 5 | image: Visual Studio 2019 6 | 7 | clone_depth: 1 8 | pull_requests: 9 | do_not_increment_build_number: true 10 | 11 | init: 12 | - ps: | 13 | git config --global core.autocrlf input 14 | 15 | if ($env:APPVEYOR_REPO_TAG -eq "true") { 16 | $ver = $env:APPVEYOR_REPO_TAG_NAME 17 | if($ver.StartsWith("v") -eq $true) { $ver = $ver.Substring(1) } 18 | Update-AppveyorBuild -Version $ver 19 | } 20 | 21 | environment: 22 | matrix: 23 | - BUILD_TARGET: base 24 | - BUILD_TARGET: fsharp 25 | 26 | build_script: 27 | - cmd: dotnet build src/CommandLine/ -c Release --version-suffix %APPVEYOR_BUILD_VERSION% /p:BuildTarget=%BUILD_TARGET% 28 | 29 | test_script: 30 | - cmd: dotnet test tests/CommandLine.Tests/ /p:BuildTarget=%BUILD_TARGET% 31 | 32 | after_test: 33 | - cmd: dotnet pack src/CommandLine/ -c Release --version-suffix %APPVEYOR_BUILD_VERSION% /p:BuildTarget=%BUILD_TARGET% 34 | 35 | artifacts: 36 | - path: 'src/CommandLine/bin/Release/*.nupkg' 37 | name: NuGetPackages 38 | - path: 'src/CommandLine/bin/Release/*.snupkg' 39 | name: symbol 40 | 41 | on_failure: 42 | - cmd: | 43 | tree /f /a >files.lst 44 | appveyor PushArtifact .\files.lst -DeploymentName "Failed Build File Listing" 45 | 46 | deploy: 47 | - provider: NuGet 48 | api_key: 49 | secure: llMIgYMuLHh9thyKMEAmkWraTaA9Zvcm1F8/yRwm0HCiPIt/ehR/GI4kJKyMTPyf 50 | artifact: /.*(\.|\.s)nupkg/ 51 | on: 52 | APPVEYOR_REPO_TAG: true 53 | -------------------------------------------------------------------------------- /art/CommandLine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/art/CommandLine.png -------------------------------------------------------------------------------- /art/CommandLine20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/art/CommandLine20.png -------------------------------------------------------------------------------- /art/CommandLineNuGet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/art/CommandLineNuGet.png -------------------------------------------------------------------------------- /art/CommandLinePreRelNuGet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/art/CommandLinePreRelNuGet.png -------------------------------------------------------------------------------- /art/resharper-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/art/resharper-logo.png -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/Application.Designer.vb: -------------------------------------------------------------------------------- 1 | '------------------------------------------------------------------------------ 2 | ' 3 | ' This code was generated by a tool. 4 | ' Runtime Version:4.0.30319.42000 5 | ' 6 | ' Changes to this file may cause incorrect behavior and will be lost if 7 | ' the code is regenerated. 8 | ' 9 | '------------------------------------------------------------------------------ 10 | 11 | Option Strict On 12 | Option Explicit On 13 | 14 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/Application.myapp: -------------------------------------------------------------------------------- 1 | 2 | 3 | false 4 | false 5 | 0 6 | true 7 | 0 8 | 2 9 | true 10 | 11 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/AssemblyInfo.vb: -------------------------------------------------------------------------------- 1 | Imports System 2 | Imports System.Reflection 3 | Imports System.Runtime.InteropServices 4 | 5 | ' General Information about an assembly is controlled through the following 6 | ' set of attributes. Change these attribute values to modify the information 7 | ' associated with an assembly. 8 | 9 | ' Review the values of the assembly attributes 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 'The following GUID is for the ID of the typelib if this project is exposed to COM 21 | 22 | 23 | ' Version information for an assembly consists of the following four values: 24 | ' 25 | ' Major Version 26 | ' Minor Version 27 | ' Build Number 28 | ' Revision 29 | ' 30 | ' You can specify all the values or you can default the Build and Revision Numbers 31 | ' by using the '*' as shown below: 32 | ' 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/Resources.Designer.vb: -------------------------------------------------------------------------------- 1 | '------------------------------------------------------------------------------ 2 | ' 3 | ' This code was generated by a tool. 4 | ' Runtime Version:4.0.30319.42000 5 | ' 6 | ' Changes to this file may cause incorrect behavior and will be lost if 7 | ' the code is regenerated. 8 | ' 9 | '------------------------------------------------------------------------------ 10 | 11 | Option Strict On 12 | Option Explicit On 13 | 14 | Imports System 15 | 16 | Namespace My.Resources 17 | 18 | 'This class was auto-generated by the StronglyTypedResourceBuilder 19 | 'class via a tool like ResGen or Visual Studio. 20 | 'To add or remove a member, edit your .ResX file then rerun ResGen 21 | 'with the /str option, or rebuild your VS project. 22 | ''' 23 | ''' A strongly-typed resource class, for looking up localized strings, etc. 24 | ''' 25 | _ 29 | Friend Module Resources 30 | 31 | Private resourceMan As Global.System.Resources.ResourceManager 32 | 33 | Private resourceCulture As Global.System.Globalization.CultureInfo 34 | 35 | ''' 36 | ''' Returns the cached ResourceManager instance used by this class. 37 | ''' 38 | _ 39 | Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager 40 | Get 41 | If Object.ReferenceEquals(resourceMan, Nothing) Then 42 | Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("ReadText.Demo.VB.Resources", GetType(Resources).Assembly) 43 | resourceMan = temp 44 | End If 45 | Return resourceMan 46 | End Get 47 | End Property 48 | 49 | ''' 50 | ''' Overrides the current thread's CurrentUICulture property for all 51 | ''' resource lookups using this strongly typed resource class. 52 | ''' 53 | _ 54 | Friend Property Culture() As Global.System.Globalization.CultureInfo 55 | Get 56 | Return resourceCulture 57 | End Get 58 | Set 59 | resourceCulture = value 60 | End Set 61 | End Property 62 | End Module 63 | End Namespace 64 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/Settings.Designer.vb: -------------------------------------------------------------------------------- 1 | '------------------------------------------------------------------------------ 2 | ' 3 | ' This code was generated by a tool. 4 | ' Runtime Version:4.0.30319.42000 5 | ' 6 | ' Changes to this file may cause incorrect behavior and will be lost if 7 | ' the code is regenerated. 8 | ' 9 | '------------------------------------------------------------------------------ 10 | 11 | Option Strict On 12 | Option Explicit On 13 | 14 | 15 | Namespace My 16 | 17 | _ 20 | Partial Friend NotInheritable Class MySettings 21 | Inherits Global.System.Configuration.ApplicationSettingsBase 22 | 23 | Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings()),MySettings) 24 | 25 | #Region "My.Settings Auto-Save Functionality" 26 | #If _MyType = "WindowsForms" Then 27 | Private Shared addedHandler As Boolean 28 | 29 | Private Shared addedHandlerLockObject As New Object 30 | 31 | _ 32 | Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs) 33 | If My.Application.SaveMySettingsOnExit Then 34 | My.Settings.Save() 35 | End If 36 | End Sub 37 | #End If 38 | #End Region 39 | 40 | Public Shared ReadOnly Property [Default]() As MySettings 41 | Get 42 | 43 | #If _MyType = "WindowsForms" Then 44 | If Not addedHandler Then 45 | SyncLock addedHandlerLockObject 46 | If Not addedHandler Then 47 | AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings 48 | addedHandler = True 49 | End If 50 | End SyncLock 51 | End If 52 | #End If 53 | Return defaultInstance 54 | End Get 55 | End Property 56 | End Class 57 | End Namespace 58 | 59 | Namespace My 60 | 61 | _ 64 | Friend Module MySettingsProperty 65 | 66 | _ 67 | Friend ReadOnly Property Settings() As Global.ReadText.Demo.VB.My.MySettings 68 | Get 69 | Return Global.ReadText.Demo.VB.My.MySettings.Default 70 | End Get 71 | End Property 72 | End Module 73 | End Namespace 74 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/My Project/app.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 52 | 59 | 60 | 61 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/Options.vb: -------------------------------------------------------------------------------- 1 | Imports CommandLine 2 | Imports CommandLine.Text 3 | 4 | Public Interface IOptions 5 | 6 | <[Option]("n"c, "lines", SetName:="bylines", [Default]:=5UI, HelpText:="Lines to be printed from the beginning or end of the file.")> 7 | Property Lines As UInteger? 8 | 9 | <[Option]("c"c, "bytes", SetName:="bybytes", HelpText:="Bytes to be printed from the beginning or end of the file.")> 10 | Property Bytes As UInteger? 11 | 12 | <[Option]("q"c, "quiet", HelpText:="Suppresses summary messages.")> 13 | Property Quiet As Boolean 14 | 15 | <[Value](0, MetaName:="input file", Required:=True, HelpText:="Input file to be processed.")> 16 | Property FileName As String 17 | 18 | End Interface 19 | 20 | <[Verb]("head", HelpText:="Displays first lines of a file.")> 21 | Public Class HeadOptions 22 | Implements IOptions 23 | Public Property Lines As UInteger? Implements IOptions.Lines 24 | 25 | Public Property Bytes As UInteger? Implements IOptions.Bytes 26 | 27 | Public Property Quiet As Boolean Implements IOptions.Quiet 28 | 29 | Public Property FileName As String Implements IOptions.FileName 30 | 31 | 32 | Public Shared ReadOnly Iterator Property IEnumerable() As IEnumerable(Of Example) 33 | Get 34 | Yield New Example("normal scenario", New HeadOptions With {.FileName = "file.bin"}) 35 | Yield New Example("specify bytes", New HeadOptions With {.FileName = "file.bin", .Bytes = 100}) 36 | Yield New Example("suppress summary", UnParserSettings.WithGroupSwitchesOnly(), New HeadOptions With {.FileName = "file.bin", .Quiet = True}) 37 | Yield New Example("read more lines", New UnParserSettings() {UnParserSettings.WithGroupSwitchesOnly(), UnParserSettings.WithUseEqualTokenOnly()}, New HeadOptions With {.FileName = "file.bin", .Lines = 10}) 38 | End Get 39 | End Property 40 | 41 | End Class 42 | 43 | <[Verb]("tail", HelpText:="Displays last lines of a file.")> 44 | Public Class TailOptions 45 | Implements IOptions 46 | Public Property Lines As UInteger? Implements IOptions.Lines 47 | 48 | Public Property Bytes As UInteger? Implements IOptions.Bytes 49 | 50 | Public Property Quiet As Boolean Implements IOptions.Quiet 51 | 52 | Public Property FileName As String Implements IOptions.FileName 53 | 54 | End Class 55 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/Program.vb: -------------------------------------------------------------------------------- 1 | Imports System.IO 2 | Imports System.Text 3 | Imports CommandLine 4 | 5 | Module Program 6 | 7 | Function Main(ByVal sArgs() As String) As Integer 8 | 9 | Dim reader As Func(Of IOptions, String) = Function(opts) 10 | Dim fromTop = opts.[GetType]() = GetType(HeadOptions) 11 | Return If(opts.Lines.HasValue, ReadLines(opts.FileName, fromTop, CInt(opts.Lines)), ReadBytes(opts.FileName, fromTop, CInt(opts.Bytes))) 12 | End Function 13 | 14 | Dim header As Func(Of IOptions, String) = Function(opts) 15 | If opts.Quiet Then Return String.Empty 16 | 17 | Dim fromTop = opts.[GetType]() = GetType(HeadOptions) 18 | Dim builder = New StringBuilder("Reading ") 19 | builder = If(opts.Lines.HasValue, builder.Append(opts.Lines).Append(" lines"), builder.Append(opts.Bytes).Append(" bytes")) 20 | builder = If(fromTop, builder.Append(" from top:"), builder.Append(" from bottom:")) 21 | Return builder.ToString() 22 | 23 | End Function 24 | 25 | Dim printIfNotEmpty As Action(Of String) = Sub(text) 26 | If text.Length = 0 Then Return 27 | Console.WriteLine(text) 28 | End Sub 29 | 30 | Dim result = Parser.Default.ParseArguments(Of HeadOptions, TailOptions)(sArgs) 31 | 32 | Dim texts = result.MapResult( 33 | Function(opts As HeadOptions) Tuple.Create(header(opts), reader(opts)), 34 | Function(opts As TailOptions) Tuple.Create(header(opts), reader(opts)), 35 | Function() MakeError()) 36 | 37 | printIfNotEmpty(texts.Item1) 38 | printIfNotEmpty(texts.Item2) 39 | 40 | Return If(texts.Equals(MakeError()), 1, 0) 41 | 42 | End Function 43 | 44 | Private Function ReadLines(fileName As String, fromTop As Boolean, count As Integer) As String 45 | 46 | Dim lines = File.ReadAllLines(fileName) 47 | If (fromTop) Then 48 | Return String.Join(Environment.NewLine, lines.Take(count)) 49 | End If 50 | 51 | Return String.Join(Environment.NewLine, lines.Reverse().Take(count)) 52 | 53 | End Function 54 | 55 | Private Function ReadBytes(fileName As String, fromTop As Boolean, count As Integer) As String 56 | 57 | Dim bytes = File.ReadAllBytes(fileName) 58 | If (fromTop) Then 59 | Return Encoding.UTF8.GetString(bytes, 0, count) 60 | End If 61 | 62 | Return Encoding.UTF8.GetString(bytes, bytes.Length - count, count) 63 | 64 | End Function 65 | 66 | Private Function MakeError() As Tuple(Of String, String) 67 | 68 | Return Tuple.Create(vbNullChar, vbNullChar) 69 | 70 | End Function 71 | 72 | End Module 73 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/ReadText.Demo.VB.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ReadText.Demo.VB", "ReadText.Demo.VB.vbproj", "{298FBAFF-C828-4BF9-8E93-2BE925E9E223}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {298FBAFF-C828-4BF9-8E93-2BE925E9E223}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {298FBAFF-C828-4BF9-8E93-2BE925E9E223}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {298FBAFF-C828-4BF9-8E93-2BE925E9E223}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {298FBAFF-C828-4BF9-8E93-2BE925E9E223}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/ReadText.Demo.VB.vbproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | publish\ 5 | 6 | 7 | 8 | 9 | 10 | en-US 11 | false 12 | 13 | -------------------------------------------------------------------------------- /demo/ReadText.Demo.VB/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/Options.cs: -------------------------------------------------------------------------------- 1 | using CommandLine; 2 | using CommandLine.Text; 3 | using System.Collections.Generic; 4 | 5 | namespace ReadText.Demo 6 | { 7 | interface IOptions 8 | { 9 | [Option('n', "lines", 10 | Default = 5U, 11 | SetName = "bylines", 12 | HelpText = "Lines to be printed from the beginning or end of the file.")] 13 | uint? Lines { get; set; } 14 | 15 | [Option('c', "bytes", 16 | SetName = "bybytes", 17 | HelpText = "Bytes to be printed from the beginning or end of the file.")] 18 | uint? Bytes { get; set; } 19 | 20 | [Option('q', "quiet", 21 | HelpText = "Suppresses summary messages.")] 22 | bool Quiet { get; set; } 23 | 24 | [Value(0, MetaName = "input file", 25 | HelpText = "Input file to be processed.", 26 | Required = true)] 27 | string FileName { get; set; } 28 | } 29 | 30 | [Verb("head", true, HelpText = "Displays first lines of a file.")] 31 | class HeadOptions : IOptions 32 | { 33 | public uint? Lines { get; set; } 34 | 35 | public uint? Bytes { get; set; } 36 | 37 | public bool Quiet { get; set; } 38 | 39 | public string FileName { get; set; } 40 | 41 | [Usage(ApplicationAlias = "ReadText.Demo.exe")] 42 | public static IEnumerable Examples 43 | { 44 | get 45 | { 46 | yield return new Example("normal scenario", new HeadOptions { FileName = "file.bin"}); 47 | yield return new Example("specify bytes", new HeadOptions { FileName = "file.bin", Bytes=100 }); 48 | yield return new Example("suppress summary", UnParserSettings.WithGroupSwitchesOnly(), new HeadOptions { FileName = "file.bin", Quiet = true }); 49 | yield return new Example("read more lines", new[] { UnParserSettings.WithGroupSwitchesOnly(), UnParserSettings.WithUseEqualTokenOnly() }, new HeadOptions { FileName = "file.bin", Lines = 10 }); 50 | } 51 | } 52 | } 53 | 54 | [Verb("tail", HelpText = "Displays last lines of a file.")] 55 | class TailOptions : IOptions 56 | { 57 | public uint? Lines { get; set; } 58 | 59 | public uint? Bytes { get; set; } 60 | 61 | public bool Quiet { get; set; } 62 | 63 | public string FileName { get; set; } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using CommandLine; 7 | using CommandLine.Text; 8 | 9 | namespace ReadText.Demo 10 | { 11 | class Program 12 | { 13 | public static int Main(string[] args) 14 | { 15 | Func reader = opts => 16 | { 17 | var fromTop = opts.GetType() == typeof(HeadOptions); 18 | return opts.Lines.HasValue 19 | ? ReadLines(opts.FileName, fromTop, (int)opts.Lines) 20 | : ReadBytes(opts.FileName, fromTop, (int)opts.Bytes); 21 | }; 22 | Func header = opts => 23 | { 24 | if (opts.Quiet) 25 | { 26 | return string.Empty; 27 | } 28 | var fromTop = opts.GetType() == typeof(HeadOptions); 29 | var builder = new StringBuilder("Reading "); 30 | builder = opts.Lines.HasValue 31 | ? builder.Append(opts.Lines).Append(" lines") 32 | : builder.Append(opts.Bytes).Append(" bytes"); 33 | builder = fromTop ? builder.Append(" from top:") : builder.Append(" from bottom:"); 34 | return builder.ToString(); 35 | }; 36 | Action printIfNotEmpty = text => 37 | { 38 | if (text.Length == 0) { return; } 39 | Console.WriteLine(text); 40 | }; 41 | 42 | var result = Parser.Default.ParseArguments(args); 43 | var texts = result 44 | .MapResult( 45 | (HeadOptions opts) => Tuple.Create(header(opts), reader(opts)), 46 | (TailOptions opts) => Tuple.Create(header(opts), reader(opts)), 47 | _ => MakeError()); 48 | 49 | printIfNotEmpty(texts.Item1); 50 | printIfNotEmpty(texts.Item2); 51 | 52 | return texts.Equals(MakeError()) ? 1 : 0; 53 | } 54 | 55 | private static string ReadLines(string fileName, bool fromTop, int count) 56 | { 57 | var lines = File.ReadAllLines(fileName); 58 | if (fromTop) 59 | { 60 | return string.Join(Environment.NewLine, lines.Take(count)); 61 | } 62 | return string.Join(Environment.NewLine, lines.Reverse().Take(count)); 63 | } 64 | 65 | private static string ReadBytes(string fileName, bool fromTop, int count) 66 | { 67 | var bytes = File.ReadAllBytes(fileName); 68 | if (fromTop) 69 | { 70 | return Encoding.UTF8.GetString(bytes, 0, count); 71 | } 72 | return Encoding.UTF8.GetString(bytes, bytes.Length - count, count); 73 | } 74 | 75 | private static Tuple MakeError() 76 | { 77 | return Tuple.Create("\0", "\0"); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | [assembly: AssemblyTitle("ReadText.Demo")] 5 | [assembly: AssemblyDescription("ReadText.Demo for Command Line Parser Library")] 6 | [assembly: AssemblyTrademark("")] 7 | #if DEBUG 8 | [assembly: AssemblyConfiguration("Debug")] 9 | #else 10 | [assembly: AssemblyConfiguration("Release")] 11 | #endif 12 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/ReadText.Demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | net40;net45;net461;netcoreapp2.1;netcoreapp2.0 5 | false 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/ReadText.Demo.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.106 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReadText.Demo", "ReadText.Demo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine", "..\..\src\CommandLine\CommandLine.csproj", "{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}" 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 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {FF14CDF0-EF51-448B-918C-47CD369568DF} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /demo/ReadText.Demo/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/.cr/personal/Navigation/RecentFilesHistory.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | LocalizableAttributeProperty.cs 7 | d:\work\arci\commandline\src\commandline\infrastructure\localizableattributeproperty.cs 8 | 9 | Infrastructure 10 | 11 | d:\WORK\ARCI\commandline\src\CommandLine\CommandLine.csproj 12 | CommandLine 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/Options.cs: -------------------------------------------------------------------------------- 1 | using CommandLine; 2 | using CommandLine.Text; 3 | using System.Collections.Generic; 4 | 5 | namespace ReadText.LocalizedDemo 6 | { 7 | interface IOptions 8 | { 9 | [Option('n', "lines", 10 | Default = 5U, 11 | SetName = "bylines", 12 | HelpText = "HelpTextLines", 13 | ResourceType = typeof(Properties.Resources))] 14 | uint? Lines { get; set; } 15 | 16 | [Option('c', "bytes", 17 | SetName = "bybytes", 18 | HelpText = "HelpTextBytes", 19 | ResourceType = typeof(Properties.Resources))] 20 | uint? Bytes { get; set; } 21 | 22 | [Option('q', "quiet", 23 | HelpText = "HelpTextQuiet", 24 | ResourceType = typeof(Properties.Resources))] 25 | bool Quiet { get; set; } 26 | 27 | [Value(0, MetaName = "input file", 28 | HelpText = "HelpTextFileName", 29 | Required = true, 30 | ResourceType = typeof(Properties.Resources))] 31 | string FileName { get; set; } 32 | } 33 | 34 | [Verb("head", HelpText = "HelpTextVerbHead", ResourceType = typeof(Properties.Resources))] 35 | class HeadOptions : IOptions 36 | { 37 | public uint? Lines { get; set; } 38 | 39 | public uint? Bytes { get; set; } 40 | 41 | public bool Quiet { get; set; } 42 | 43 | public string FileName { get; set; } 44 | 45 | [Usage(ApplicationAlias = "ReadText.LocalizedDemo.exe")] 46 | public static IEnumerable Examples 47 | { 48 | get 49 | { 50 | yield return new Example(Properties.Resources.ExamplesNormalScenario, new HeadOptions { FileName = "file.bin"}); 51 | yield return new Example(Properties.Resources.ExamplesSpecifyBytes, new HeadOptions { FileName = "file.bin", Bytes=100 }); 52 | yield return new Example(Properties.Resources.ExamplesSuppressSummary, UnParserSettings.WithGroupSwitchesOnly(), new HeadOptions { FileName = "file.bin", Quiet = true }); 53 | yield return new Example(Properties.Resources.ExamplesReadMoreLines, new[] { UnParserSettings.WithGroupSwitchesOnly(), UnParserSettings.WithUseEqualTokenOnly() }, new HeadOptions { FileName = "file.bin", Lines = 10 }); 54 | } 55 | } 56 | } 57 | 58 | [Verb("tail", HelpText = "HelpTextVerbTail", ResourceType = typeof(Properties.Resources))] 59 | class TailOptions : IOptions 60 | { 61 | public uint? Lines { get; set; } 62 | 63 | public uint? Bytes { get; set; } 64 | 65 | public bool Quiet { get; set; } 66 | 67 | public string FileName { get; set; } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using CommandLine; 7 | using CommandLine.Text; 8 | 9 | namespace ReadText.LocalizedDemo 10 | { 11 | class Program 12 | { 13 | public static int Main(string[] args) 14 | { 15 | // Set sentence builder to localizable 16 | SentenceBuilder.Factory = () => new LocalizableSentenceBuilder(); 17 | 18 | Func reader = opts => 19 | { 20 | var fromTop = opts.GetType() == typeof(HeadOptions); 21 | return opts.Lines.HasValue 22 | ? ReadLines(opts.FileName, fromTop, (int)opts.Lines) 23 | : ReadBytes(opts.FileName, fromTop, (int)opts.Bytes); 24 | }; 25 | Func header = opts => 26 | { 27 | if (opts.Quiet) 28 | { 29 | return string.Empty; 30 | } 31 | var fromTop = opts.GetType() == typeof(HeadOptions); 32 | var builder = new StringBuilder(Properties.Resources.Reading); 33 | builder = opts.Lines.HasValue 34 | ? builder.Append(opts.Lines).Append(Properties.Resources.Lines) 35 | : builder.Append(opts.Bytes).Append(Properties.Resources.Bytes); 36 | builder = fromTop ? builder.Append(Properties.Resources.FromTop) : builder.Append(Properties.Resources.FromBottom); 37 | return builder.ToString(); 38 | }; 39 | Action printIfNotEmpty = text => 40 | { 41 | if (text.Length == 0) { return; } 42 | Console.WriteLine(text); 43 | }; 44 | 45 | var result = Parser.Default.ParseArguments(args); 46 | var texts = result 47 | .MapResult( 48 | (HeadOptions opts) => Tuple.Create(header(opts), reader(opts)), 49 | (TailOptions opts) => Tuple.Create(header(opts), reader(opts)), 50 | _ => MakeError()); 51 | 52 | printIfNotEmpty(texts.Item1); 53 | printIfNotEmpty(texts.Item2); 54 | 55 | return texts.Equals(MakeError()) ? 1 : 0; 56 | } 57 | 58 | private static string ReadLines(string fileName, bool fromTop, int count) 59 | { 60 | var lines = File.ReadAllLines(fileName); 61 | if (fromTop) 62 | { 63 | return string.Join(Environment.NewLine, lines.Take(count)); 64 | } 65 | return string.Join(Environment.NewLine, lines.Reverse().Take(count)); 66 | } 67 | 68 | private static string ReadBytes(string fileName, bool fromTop, int count) 69 | { 70 | var bytes = File.ReadAllBytes(fileName); 71 | if (fromTop) 72 | { 73 | return Encoding.UTF8.GetString(bytes, 0, count); 74 | } 75 | return Encoding.UTF8.GetString(bytes, bytes.Length - count, count); 76 | } 77 | 78 | private static Tuple MakeError() 79 | { 80 | return Tuple.Create("\0", "\0"); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | [assembly: AssemblyTitle("ReadText.Demo")] 5 | [assembly: AssemblyDescription("ReadText.Demo for Command Line Parser Library")] 6 | [assembly: AssemblyTrademark("")] 7 | #if DEBUG 8 | [assembly: AssemblyConfiguration("Debug")] 9 | #else 10 | [assembly: AssemblyConfiguration("Release")] 11 | #endif 12 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "ReadText.LocalizedDemo": { 4 | "commandName": "Project", 5 | "commandLineArgs": "head" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/ReadText.LocalizedDemo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | net40;net45;net461;netcoreapp2.1;netcoreapp2.0 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | True 13 | True 14 | Resources.resx 15 | 16 | 17 | 18 | 19 | PublicResXFileCodeGenerator 20 | Resources.Designer.cs 21 | 22 | 23 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/ReadText.LocalizedDemo.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.106 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReadText.LocalizedDemo", "ReadText.LocalizedDemo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine", "..\..\src\CommandLine\CommandLine.csproj", "{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}" 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 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {FF14CDF0-EF51-448B-918C-47CD369568DF} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /demo/ReadText.LocalizedDemo/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /demo/fsharp-demo.fsx: -------------------------------------------------------------------------------- 1 | #r "./../src/CommandLine/bin/Debug/CommandLine.dll" 2 | 3 | open CommandLine 4 | open CommandLine.Text 5 | 6 | type options = { 7 | [] stringValue : string; 8 | [] intSequence : int seq; 9 | [] boolValue : bool; 10 | [] longValue : int64 option; } 11 | with 12 | [] 13 | static member examples 14 | with get() = seq { 15 | yield Example("Supply some values", {stringValue = "hello"; boolValue = true; intSequence = seq {1..3}; longValue = Some 10L }) } 16 | 17 | let formatLong o = 18 | match o with 19 | | Some(v) -> string v 20 | | _ -> "{None}" 21 | 22 | let formatInput (o : options) = 23 | sprintf "--stringvalue: %s\n-i: %A\n-x: %b\nvalue: %s\n" o.stringValue o.intSequence o.boolValue (formatLong o.longValue) 24 | 25 | let inline (|Success|Help|Version|Fail|) (result : ParserResult<'a>) = 26 | match result with 27 | | :? Parsed<'a> as parsed -> Success(parsed.Value) 28 | | :? NotParsed<'a> as notParsed when notParsed.Errors.IsHelp() -> Help 29 | | :? NotParsed<'a> as notParsed when notParsed.Errors.IsVersion() -> Version 30 | | :? NotParsed<'a> as notParsed -> Fail(notParsed.Errors) 31 | | _ -> failwith "invalid parser result" 32 | 33 | let args = fsi.CommandLineArgs.[1..] 34 | let result = Parser.Default.ParseArguments(args) 35 | 36 | match result with 37 | | Success(opts) -> printf "%s" (formatInput opts) 38 | | Fail(errs) -> printf "Invalid: %A, Errors: %u\n" args (Seq.length errs) 39 | | Help | Version -> () 40 | -------------------------------------------------------------------------------- /docs/Contributors: -------------------------------------------------------------------------------- 1 | Contributions Guidelines for Command Line Parser Library 2 | Giacomo Stelluti Scala (gsscoder@gmail.com) 3 | Last Mod.: 2013-04-26 4 | 5 | Tools: 6 | - MS FxCop (VSSTUDIO CodeAnalysis) 7 | - MS StyleCop 8 | - JetBrains R# 9 | 10 | Premise 11 | These are suggestions don't be stuck to send pull requests just because you think that something written here is not perfectly followed. 12 | It is possible (or better is almost certain) that also the writer of this document violates some rule... but he tries to adhere! 13 | The project tree includes also two ReSharper files that I try to keep in sync with StyleCop. 14 | 15 | Code Formatting 16 | - No tabs (\t), use for 4 spaces instead. 17 | - Don't overuse spaces: 18 | void Method(param1, param2, param3) -> OK 19 | void Method ( param1, param2, param3 ) -> TOO SPACES! 20 | - Align with one indentation: 21 | if (lonlongstuff1 == longlongstuff2 || (longlongstuff3 > 0 && 22 | longlongstuff4 < 0)) -> OK 23 | if (lonlongstuff1 == longlongstuff2 || (longlongstuff3 > 0 && 24 | longlongstuff4 < 0)) -> TOO INDENTATION! 25 | 26 | Coding Style 27 | - Adhere to .NET Framework Coding Guidelines (http://msdn.microsoft.com/en-us/library/ms229042.aspx) for public types. 28 | - Kernel of 2.0+ has been rewritten following Functional Programming principles (http://en.wikipedia.org/wiki/Functional_programming). 29 | - A minor use of side-effects could be allowed if isolated to "out-side" world (e.g.: see Tokenizer type). 30 | 31 | Unit Tests 32 | Personally 99% of times I write the unit tests before the desidered modification (TDD). Anyway please submit changes along with at least one unit test (this is also useful to let me understand what the change impacts). 33 | 34 | PR etiquette 35 | A not discussed PULL REQUEST could lead only to extra work for project coordinator; 36 | PR are well accepted, anyway they should be result of new or open discussions. 37 | 38 | Breaking Changes 39 | Please discuss any important modification or something that breaks the Public API. 40 | 41 | Not covered here 42 | You can take existing code as a model or write me. 43 | -------------------------------------------------------------------------------- /docs/INFO: -------------------------------------------------------------------------------- 1 | Command Line Parser Library 2 | ------------------------------------------------------------------------------------------- 3 | Project Author/Coordinator: Giacomo Stelluti Scala 4 | Main Contributors: Steven Evans, Kevin Moore, Dan Nemec (nemec), Alexander Fast (mizipzor) 5 | Others: Tom Glastonbury (tg73) 6 | ------------------------------------------------------------------------------------------- 7 | 8 | 9 | Git home: 10 | https://github.com/gsscoder/commandline 11 | 12 | General Info: 13 | This library allows command line arguments parsing. 14 | This work were inspired by the GNU LIBC getopt()/getopt_long() functions 15 | (check out: http://www.gnu.org/software/libc/manual/html_node/Getopt.html). 16 | Need a guide on option naming? Check this one: 17 | http://catb.org/~esr/writings/taoup/html/ch10s05.html. 18 | 19 | More info on: 20 | [QUICKSTART] http://commandline.codeplex.com/wikipage?title=Quickstart&referringTitle=Documentation 21 | [GUIDE] http://commandline.codeplex.com/documentation (*2) 22 | [BLOG] http://gsscoder.github.com/ 23 | 24 | Build (requires Ruby): 25 | MonoDevelop or Visual Studio 26 | 27 | Documentation: 28 | From version 1.9.0.3 Beta CHM HelpFile was removed from {libroot}/doc. 29 | See online documentation section hosted on CodePlex project. 30 | 31 | Framework Compatibility: 32 | - C# 3.0+ compiler 33 | - .NET Framework 2.0+ 34 | - Mono 2.1+ Profile 35 | 36 | Development Environment: 37 | - OS: 38 | Ubuntu 12.04 Desktop (http://www.ubuntu.com/) 39 | Mac OS X Lion (http://www.apple.com/osx/) 40 | Microsoft Windows 7 (http://windows.microsoft.com/en-US/windows/home) 41 | - IDE: 42 | MonoDevelop (http://monodevelop.com/) 43 | MS Visual Studio 2010|2012 (http://www.microsoft.com/visualstudio/eng/visual-studio-update) 44 | - Code Analysis: 45 | JetBrains ReSharper with Open Source License (http://www.jetbrains.com/resharper/) 46 | - Version Control: 47 | Git (http://git-scm.com/) 48 | msysgit (http://code.google.com/p/msysgit/) 49 | SVN (http://subversion.tigris.org/) 50 | - Other Tools: 51 | TextMate (http://macromates.com/) 52 | Scribes (http://scribes.sourceforge.net/) 53 | 54 | Test Environment: 55 | - Mac OS X Lion 56 | - Microsoft Windows 7 57 | - Ubuntu 12.04 58 | 59 | Licence: 60 | MIT License 61 | http://www.opensource.org/licenses/mit-license.php 62 | 63 | Comments, bugs and other: 64 | gsscoder@gmail.com 65 | 66 | Enjoy, 67 | Giacomo Stelluti Scala 68 | 69 | --------------------------------------------------------------------------------------- 70 | [NOTES] 71 | *1) In a production environment you should use a 'stable' version, an 'rc' (release 72 | candidate) or an advanced 'beta' (a 'beta' published for a long time and stated quite stable). 73 | 74 | *2) Full documentation is currently under construction. Please refer to Quickstart 75 | guide and informations published starting from the project's home. 76 | --------------------------------------------------------------------------------------- 77 | -------------------------------------------------------------------------------- /docs/PublicAPI.md: -------------------------------------------------------------------------------- 1 | Public API Changes: 2 | --- 3 | - Version 1.9.4.91: Breaking, short name of an option must be defined as character (``System.Char``). Non breaking, added support for verbs. 4 | - Version 1.9.4.99: Breaking, removed dependency from ``CommandLineOptionsBase``, introduced [ParseStateAttribute](https://github.com/gsscoder/commandline/blob/master/src/sample/Program.cs). 5 | - Version 1.9.4.107: Non breaking, implemented [strict parsing](https://github.com/gsscoder/commandline/blob/master/src/tests/Parser/StrictFixture.cs) (see issue #32). 6 | - Version 1.9.4.109: Non breaking, pull request #44. 7 | - Version 1.9.4.111: Non breaking, ``CommandLineParserSettings``, ``CommandLineParser`` implements ``IDisposable``. 8 | - Version 1.9.4.113: Non breaking, added ``CommandLineParser::WasVerbOptionInvoked`` helper method. 9 | - Version 1.9.4.123: Breaking, ``HandleParsingErrorsDelegate`` renamed to ``ParsingErrorsHandler``, ``MultiLineTextAttribute`` renamed to ``MultilineTextAttribute``. Non breaking, refactoring (see ChangeLog). 10 | - Version 1.9.4.127: 'Partially' non breaking, ``OptionAttribute`` is now sealed. ``OptionArrayAttribute`` and ``OptionListAttribute`` derives from ``BaseOptionAttribute`` (update your custom types too). 11 | - Version 1.9.4.201: Non breaking, introduced ``ValueOptionAttribute`` enhancement of issue #33. 12 | - Version 1.9.4.207: Breaking: ``CommandLineParser``, ``ICommandLineParser``, ``CommandLineParserSettings``, ``CommandLineParserException`` renamed to ``Parser``, ``IParser``, ``ParserSettings``, ``ParserException`` as explained [here](https://github.com/gsscoder/commandline/issues/48). 13 | - Version 1.9.4.209: Non breaking, added fluent builder (``ParserConfigurator``, see issue #42). 14 | - Version 1.9.4.211: 'Partially' non breaking, ParsingErrorsHandler delegate replaced by Action. 15 | - Version 1.9.4.217: Non breaking, Extracted interface ``IParserSettings`` from ``ParserSettings``. Changed consumers to depends on ``IParserSettings`` rather on concrete default implementation. 16 | - Version 1.9.4.219: Non breaking, ``ValueOption`` supports ``Index`` (Remarks: in next Dtable will mandatory). 17 | - Version 1.9.4.223: Non breaking, added ``IParserSettings::ParsingCulture`` and ``ParserConfigurator::UseCulture``. 18 | - Version 1.9.4.225: Breaking, default singleton parsing culture is ``CultureInfo.InvariantCulture``; this is not general default only the one of default singleton. 19 | - Version 1.9.4.223: 'Partially' non breaking, all attributes are now in root namespace. 20 | - Version 1.9.5.0: Breaking (in some cases), removed ``IParser::ParseArguments`` overloads (see ChangeLog); removed ``::WasVerbOptionInvoked``; use new ``HelpText::AutoBuild(object,string)`` instead of obsolete ``::GetVerbOptionsInstanceByName``. 21 | - Version 1.9.6.1: Non breaking (if implicit syntax), reverting back genericity from IParser. 22 | - Version 1.9.61.1: Non breaking, omitting longname default -> property name lower case. 23 | - Version 1.9.62.2: 'Partially' breaking, ``IParserConfigurator`` made nested type of Parser; ``ParserConfigurator::HelpWriter(...)`` renamed to ``ParserConfigurator::UseHelpWriter(...)``. 24 | - Version 1.9.69.1: Breaking (in some cases), removed ``IParser``, ``IParserSettings`` and ``ParserConfigurator``. -------------------------------------------------------------------------------- /docs/content/index.fsx: -------------------------------------------------------------------------------- 1 | (*** hide ***) 2 | // This block of code is omitted in the generated HTML documentation. Use 3 | // it to define helpers that you do not want to show in the documentation. 4 | #I "../../build" 5 | #r "CommandLine.dll" 6 | 7 | open System 8 | 9 | (** 10 | 11 | # Command Line Parser Library 12 | 13 | Terse syntax C# command line parser for .NET with F# support. 14 | 15 | 16 | 17 | 18 | 19 | CommandLineParser can be installed from NuGet: 20 | PM> Install-Package CommandLineParser -Pre 21 | 22 | 23 | 24 | 25 | 26 | ## Introduction 27 | 28 | The library parses command line arguments to a record decorated with attributes: 29 | *) 30 | 31 | type options = { 32 | [] files : seq; 33 | [] verbose : bool; 34 | [] offset : int64 option; 35 | } 36 | 37 | (** 38 | 39 | Previous record defines the above parsing scheme: 40 | 41 | [lang=bash] 42 | --files file1.bin file2.txt file3.xml --verbose --offset 11 43 | 44 | and it will be used to build up this record instance: 45 | *) 46 | 47 | { files = seq { yield "file1.bin"; yield "file2.txt"; yield "file3.xml"}; verbose = true; offset = Some 11L } 48 | 49 | (** 50 | 51 | ## Who uses CommandLineParser? 52 | 53 | * [FSharp.Formatting](http://tpetricek.github.io/FSharp.Formatting/) 54 | 55 | * Various commercial products 56 | 57 | ## Documentation 58 | 59 | * [Tutorial](tutorial.html) A short walkthrough of CommandLineParser features. 60 | 61 | * [GitHub Wiki](https://github.com/gsscoder/commandline/wiki/Latest-Version) 62 | 63 | ## Contributing and copyright 64 | 65 | The project is hosted on [GitHub][gh] where you can [report issues][issues], fork 66 | the project and submit pull requests. 67 | 68 | The library is available under the MIT License. 69 | For more information see the [License file][license] in the GitHub repository. 70 | 71 | [gh]: https://github.com/gsscoder/commandline 72 | [issues]: https://github.com/gsscoder/commandline/issues 73 | [license]: https://github.com/gsscoder/commandline/blob/master/License.md 74 | 75 | *) -------------------------------------------------------------------------------- /docs/files/img/logo-template.pdn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/docs/files/img/logo-template.pdn -------------------------------------------------------------------------------- /docs/files/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/commandlineparser/commandline/1e3607b97af6141743edb3c434c06d5b492f6fb3/docs/files/img/logo.png -------------------------------------------------------------------------------- /docs/tools/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /docs/tools/templates/template.cshtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | @Title 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | fsharp.org 27 | github page 28 | 29 | @Properties["project-name"] 30 | 31 | 32 | 33 | 34 | @RenderBody() 35 | 36 | 37 | 38 | @Properties["project-name"] 39 | Home page 40 | 41 | Get Library via NuGet 42 | Source Code on GitHub 43 | License 44 | 45 | Documentation 46 | Tutorial 47 | API Reference 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/CommandLine/Core/ArgumentsExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | 8 | namespace CommandLine.Core 9 | { 10 | static class ArgumentsExtensions 11 | { 12 | public static IEnumerable Preprocess( 13 | this IEnumerable arguments, 14 | IEnumerable< 15 | Func, IEnumerable> 16 | > preprocessorLookup) 17 | { 18 | return preprocessorLookup.TryHead().MapValueOrDefault( 19 | func => 20 | { 21 | var errors = func(arguments); 22 | return errors.Any() 23 | ? errors 24 | : arguments.Preprocess(preprocessorLookup.TailNoFail()); 25 | }, 26 | Enumerable.Empty()); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/CommandLine/Core/KeyValuePairHelper.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using CommandLine.Infrastructure; 4 | using CSharpx; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace CommandLine.Core 9 | { 10 | static class KeyValuePairHelper 11 | { 12 | public static IEnumerable>> ForSwitch( 13 | IEnumerable tokens) 14 | { 15 | return tokens.Select(t => t.Text.ToKeyValuePair("true")); 16 | } 17 | 18 | public static IEnumerable>> ForScalar( 19 | IEnumerable tokens) 20 | { 21 | return tokens 22 | .Group(2) 23 | .Select((g) => g[0].Text.ToKeyValuePair(g[1].Text)); 24 | } 25 | 26 | public static IEnumerable>> ForSequence( 27 | IEnumerable tokens) 28 | { 29 | return from t in tokens.Pairwise( 30 | (f, s) => 31 | f.IsName() 32 | ? f.Text.ToKeyValuePair(tokens.SkipWhile(t => !t.Equals(f)).SkipWhile(t => t.Equals(f)).TakeWhile(v => v.IsValue()).Select(x => x.Text).ToArray()) 33 | : string.Empty.ToKeyValuePair()) 34 | where t.Key.Length > 0 && t.Value.Any() 35 | select t; 36 | } 37 | 38 | private static KeyValuePair> ToKeyValuePair(this string value, params string[] values) 39 | { 40 | return new KeyValuePair>(value, values); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/CommandLine/Core/NameExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Core 6 | { 7 | static class NameExtensions 8 | { 9 | public static bool MatchName(this string value, string shortName, string longName, StringComparer comparer) 10 | { 11 | return value.Length == 1 12 | ? comparer.Equals(value, shortName) 13 | : comparer.Equals(value, longName); 14 | } 15 | 16 | public static NameInfo FromOptionSpecification(this OptionSpecification specification) 17 | { 18 | return new NameInfo( 19 | specification.ShortName, 20 | specification.LongName); 21 | } 22 | 23 | public static NameInfo FromSpecification(this Specification specification) 24 | { 25 | switch (specification.Tag) 26 | { 27 | case SpecificationType.Option: 28 | return FromOptionSpecification((OptionSpecification)specification); 29 | default: 30 | return NameInfo.EmptyName; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/CommandLine/Core/NameLookup.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | 8 | namespace CommandLine.Core 9 | { 10 | enum NameLookupResult 11 | { 12 | NoOptionFound, 13 | BooleanOptionFound, 14 | OtherOptionFound 15 | } 16 | 17 | static class NameLookup 18 | { 19 | public static NameLookupResult Contains(string name, IEnumerable specifications, StringComparer comparer) 20 | { 21 | var option = specifications.FirstOrDefault(a => name.MatchName(a.ShortName, a.LongName, comparer)); 22 | if (option == null) return NameLookupResult.NoOptionFound; 23 | return option.ConversionType == typeof(bool) || (option.ConversionType == typeof(int) && option.FlagCounter) 24 | ? NameLookupResult.BooleanOptionFound 25 | : NameLookupResult.OtherOptionFound; 26 | } 27 | 28 | public static Maybe HavingSeparator(string name, IEnumerable specifications, 29 | StringComparer comparer) 30 | { 31 | return specifications.SingleOrDefault( 32 | a => name.MatchName(a.ShortName, a.LongName, comparer) && a.Separator != '\0') 33 | .ToMaybe() 34 | .MapValueOrDefault(spec => Maybe.Just(spec.Separator), Maybe.Nothing()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CommandLine/Core/OptionMapper.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | using RailwaySharp.ErrorHandling; 8 | 9 | namespace CommandLine.Core 10 | { 11 | static class OptionMapper 12 | { 13 | public static Result< 14 | IEnumerable, Error> 15 | MapValues( 16 | IEnumerable propertyTuples, 17 | IEnumerable>> options, 18 | Func, Type, bool, bool, Maybe> converter, 19 | StringComparer comparer) 20 | { 21 | var sequencesAndErrors = propertyTuples 22 | .Select( 23 | pt => 24 | { 25 | var matched = options.Where(s => 26 | s.Key.MatchName(((OptionSpecification)pt.Specification).ShortName, ((OptionSpecification)pt.Specification).LongName, comparer)).ToMaybe(); 27 | if (matched.IsJust()) 28 | { 29 | var matches = matched.GetValueOrDefault(Enumerable.Empty>>()); 30 | var values = new List(); 31 | foreach (var kvp in matches) 32 | { 33 | foreach (var value in kvp.Value) 34 | { 35 | values.Add(value); 36 | } 37 | } 38 | 39 | bool isFlag = pt.Specification.Tag == SpecificationType.Option && ((OptionSpecification)pt.Specification).FlagCounter; 40 | 41 | return converter(values, isFlag ? typeof(bool) : pt.Property.PropertyType, pt.Specification.TargetType != TargetType.Sequence, isFlag) 42 | .Select(value => Tuple.Create(pt.WithValue(Maybe.Just(value)), Maybe.Nothing())) 43 | .GetValueOrDefault( 44 | Tuple.Create>( 45 | pt, 46 | Maybe.Just( 47 | new BadFormatConversionError( 48 | ((OptionSpecification)pt.Specification).FromOptionSpecification())))); 49 | } 50 | 51 | return Tuple.Create(pt, Maybe.Nothing()); 52 | } 53 | ).Memoize(); 54 | return Result.Succeed( 55 | sequencesAndErrors.Select(se => se.Item1), 56 | sequencesAndErrors.Select(se => se.Item2).OfType>().Select(se => se.Value)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/CommandLine/Core/PartitionExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | 8 | namespace CommandLine.Core 9 | { 10 | static class PartitionExtensions 11 | { 12 | public static Tuple,IEnumerable> PartitionByPredicate( 13 | this IEnumerable items, 14 | Func pred) 15 | { 16 | List yes = new List(); 17 | List no = new List(); 18 | foreach (T item in items) { 19 | List list = pred(item) ? yes : no; 20 | list.Add(item); 21 | } 22 | return Tuple.Create,IEnumerable>(yes, no); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/CommandLine/Core/PreprocessorGuards.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace CommandLine.Core 8 | { 9 | static class PreprocessorGuards 10 | { 11 | public static IEnumerable, IEnumerable>> 12 | Lookup(StringComparer nameComparer, bool autoHelp, bool autoVersion) 13 | { 14 | var list = new List, IEnumerable>>(); 15 | if (autoHelp) 16 | list.Add(HelpCommand(nameComparer)); 17 | if (autoVersion) 18 | list.Add(VersionCommand(nameComparer)); 19 | return list; 20 | } 21 | 22 | public static Func, IEnumerable> HelpCommand(StringComparer nameComparer) 23 | { 24 | return 25 | arguments => 26 | nameComparer.Equals("--help", arguments.First()) 27 | ? new Error[] { new HelpRequestedError() } 28 | : Enumerable.Empty(); 29 | } 30 | 31 | public static Func, IEnumerable> VersionCommand(StringComparer nameComparer) 32 | { 33 | return 34 | arguments => 35 | nameComparer.Equals("--version", arguments.First()) 36 | ? new Error[] { new VersionRequestedError() } 37 | : Enumerable.Empty(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/CommandLine/Core/SpecificationGuards.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using CSharpx; 6 | 7 | namespace CommandLine.Core 8 | { 9 | static class SpecificationGuards 10 | { 11 | public static readonly IEnumerable, string>> Lookup = new List, string>> 12 | { 13 | Tuple.Create(GuardAgainstScalarWithRange(), "Scalar option specifications do not support range specification."), 14 | Tuple.Create(GuardAgainstSequenceWithWrongRange(), "Bad range in sequence option specifications."), 15 | Tuple.Create(GuardAgainstSequenceWithZeroRange(), "Zero is not allowed in range of sequence option specifications."), 16 | Tuple.Create(GuardAgainstOneCharLongName(), "Long name should be longer than one character.") 17 | }; 18 | 19 | private static Func GuardAgainstScalarWithRange() 20 | { 21 | return spec => spec.TargetType == TargetType.Scalar 22 | && (spec.Min.IsJust() || spec.Max.IsJust()); 23 | } 24 | 25 | private static Func GuardAgainstSequenceWithWrongRange() 26 | { 27 | return spec => spec.TargetType == TargetType.Sequence 28 | && spec.HavingRange((min, max) => min > max); 29 | } 30 | 31 | private static Func GuardAgainstOneCharLongName() 32 | { 33 | return spec => spec.IsOption() && ((OptionSpecification)spec).LongName.Length == 1; 34 | } 35 | 36 | private static Func GuardAgainstSequenceWithZeroRange() 37 | { 38 | return spec => spec.TargetType == TargetType.Sequence 39 | && (spec.HavingMin(min => min == 0) 40 | || spec.HavingMax(max => max == 0)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/CommandLine/Core/SpecificationProperty.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Reflection; 5 | using CSharpx; 6 | 7 | namespace CommandLine.Core 8 | { 9 | class SpecificationProperty 10 | { 11 | private readonly Specification specification; 12 | private readonly PropertyInfo property; 13 | private readonly Maybe value; 14 | 15 | private SpecificationProperty(Specification specification, PropertyInfo property, Maybe value) 16 | { 17 | this.property = property; 18 | this.specification = specification; 19 | this.value = value; 20 | } 21 | 22 | public static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe value) 23 | { 24 | if (value == null) throw new ArgumentNullException("value"); 25 | 26 | return new SpecificationProperty(specification, property, value); 27 | } 28 | 29 | public Specification Specification 30 | { 31 | get { return specification; } 32 | } 33 | 34 | public PropertyInfo Property 35 | { 36 | get { return property; } 37 | } 38 | 39 | public Maybe Value 40 | { 41 | get { return value; } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/CommandLine/Core/SpecificationPropertyExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | using System.Reflection; 8 | 9 | namespace CommandLine.Core 10 | { 11 | static class SpecificationPropertyExtensions 12 | { 13 | public static SpecificationProperty WithSpecification(this SpecificationProperty specProp, Specification newSpecification) 14 | { 15 | if (newSpecification == null) throw new ArgumentNullException(nameof(newSpecification)); 16 | 17 | return SpecificationProperty.Create(newSpecification, specProp.Property, specProp.Value); 18 | } 19 | 20 | public static SpecificationProperty WithValue(this SpecificationProperty specProp, Maybe newValue) 21 | { 22 | if (newValue == null) throw new ArgumentNullException(nameof(newValue)); 23 | 24 | return SpecificationProperty.Create(specProp.Specification, specProp.Property, newValue); 25 | } 26 | 27 | public static Type GetConversionType(this SpecificationProperty specProp) 28 | { 29 | switch (specProp.Specification.TargetType) 30 | { 31 | case TargetType.Sequence: 32 | return specProp.Property.PropertyType.GetTypeInfo().GetGenericArguments() 33 | .SingleOrDefault() 34 | .ToMaybe() 35 | .FromJustOrFail( 36 | new InvalidOperationException("Sequence properties should be of type IEnumerable.")); 37 | default: 38 | return specProp.Property.PropertyType; 39 | } 40 | } 41 | 42 | public static IEnumerable Validate( 43 | this IEnumerable specProps, 44 | IEnumerable, 45 | IEnumerable>> rules) 46 | { 47 | return rules.SelectMany(rule => rule(specProps)); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/CommandLine/Core/TypeDescriptor.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using CSharpx; 5 | 6 | namespace CommandLine.Core 7 | { 8 | struct TypeDescriptor 9 | { 10 | private readonly TargetType targetType; 11 | private readonly Maybe maxItems; 12 | private readonly Maybe nextValue; 13 | 14 | private TypeDescriptor(TargetType targetType, Maybe maxItems, Maybe nextValue = null) 15 | { 16 | this.targetType = targetType; 17 | this.maxItems = maxItems; 18 | this.nextValue = nextValue; 19 | } 20 | 21 | public TargetType TargetType 22 | { 23 | get { return targetType; } 24 | } 25 | 26 | public Maybe MaxItems 27 | { 28 | get { return maxItems; } 29 | } 30 | 31 | public Maybe NextValue 32 | { 33 | get { return this.nextValue; } 34 | } 35 | 36 | public static TypeDescriptor Create(TargetType tag, Maybe maximumItems, TypeDescriptor next = default(TypeDescriptor)) 37 | { 38 | if (maximumItems == null) throw new ArgumentNullException("maximumItems"); 39 | 40 | return new TypeDescriptor(tag, maximumItems, next.ToMaybe()); 41 | } 42 | } 43 | 44 | static class TypeDescriptorExtensions 45 | { 46 | public static TypeDescriptor WithNextValue(this TypeDescriptor descriptor, Maybe nextValue) 47 | { 48 | return TypeDescriptor.Create(descriptor.TargetType, descriptor.MaxItems, nextValue.GetValueOrDefault(default(TypeDescriptor))); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /src/CommandLine/Core/TypeLookup.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using CSharpx; 7 | 8 | namespace CommandLine.Core 9 | { 10 | static class TypeLookup 11 | { 12 | public static Maybe FindTypeDescriptorAndSibling( 13 | string name, 14 | IEnumerable specifications, 15 | StringComparer comparer) 16 | { 17 | var info = 18 | specifications.SingleOrDefault(a => name.MatchName(a.ShortName, a.LongName, comparer)) 19 | .ToMaybe() 20 | .Map( 21 | first => 22 | { 23 | var descr = TypeDescriptor.Create(first.TargetType, first.Max); 24 | var next = specifications 25 | .SkipWhile(s => s.Equals(first)).Take(1) 26 | .SingleOrDefault(x => x.IsValue()).ToMaybe() 27 | .Map(second => TypeDescriptor.Create(second.TargetType, second.Max)); 28 | return descr.WithNextValue(next); 29 | }); 30 | return info; 31 | 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/CommandLine/Core/ValueSpecification.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using CSharpx; 6 | 7 | namespace CommandLine.Core 8 | { 9 | sealed class ValueSpecification : Specification 10 | { 11 | private readonly int index; 12 | private readonly string metaName; 13 | 14 | public ValueSpecification(int index, string metaName, bool required, Maybe min, Maybe max, Maybe defaultValue, 15 | string helpText, string metaValue, IEnumerable enumValues, 16 | Type conversionType, TargetType targetType, bool hidden = false) 17 | : base(SpecificationType.Value, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, targetType, hidden) 18 | { 19 | this.index = index; 20 | this.metaName = metaName; 21 | } 22 | 23 | public static ValueSpecification FromAttribute(ValueAttribute attribute, Type conversionType, IEnumerable enumValues) 24 | { 25 | return new ValueSpecification( 26 | attribute.Index, 27 | attribute.MetaName, 28 | attribute.Required, 29 | attribute.Min == -1 ? Maybe.Nothing() : Maybe.Just(attribute.Min), 30 | attribute.Max == -1 ? Maybe.Nothing() : Maybe.Just(attribute.Max), 31 | attribute.Default.ToMaybe(), 32 | attribute.HelpText, 33 | attribute.MetaValue, 34 | enumValues, 35 | conversionType, 36 | conversionType.ToTargetType(), 37 | attribute.Hidden); 38 | } 39 | 40 | public int Index 41 | { 42 | get { return index; } 43 | } 44 | 45 | public string MetaName 46 | { 47 | get { return metaName;} 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/CommandLine/Core/Verb.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | 8 | namespace CommandLine.Core 9 | { 10 | sealed class Verb 11 | { 12 | public Verb(string name, string helpText, bool hidden, bool isDefault, string[] aliases) 13 | { 14 | if (string.IsNullOrWhiteSpace(name)) 15 | throw new ArgumentNullException(nameof(name)); 16 | Name = name; 17 | 18 | HelpText = helpText ?? throw new ArgumentNullException(nameof(helpText)); 19 | Hidden = hidden; 20 | IsDefault = isDefault; 21 | Aliases = aliases ?? new string[0]; 22 | } 23 | 24 | public string Name { get; private set; } 25 | 26 | public string HelpText { get; private set; } 27 | 28 | public bool Hidden { get; private set; } 29 | 30 | public bool IsDefault { get; private set; } 31 | 32 | public string[] Aliases { get; private set; } 33 | 34 | public static Verb FromAttribute(VerbAttribute attribute) 35 | { 36 | return new Verb( 37 | attribute.Name, 38 | attribute.HelpText, 39 | attribute.Hidden, 40 | attribute.IsDefault, 41 | attribute.Aliases 42 | ); 43 | } 44 | 45 | public static IEnumerable> SelectFromTypes(IEnumerable types) 46 | { 47 | return from type in types 48 | let attrs = type.GetTypeInfo().GetCustomAttributes(typeof(VerbAttribute), true).ToArray() 49 | where attrs.Length == 1 50 | select Tuple.Create( 51 | FromAttribute((VerbAttribute)attrs.Single()), 52 | type); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/CommandLine/ErrorExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using CommandLine.Core; 6 | using CommandLine.Infrastructure; 7 | 8 | namespace CommandLine 9 | { 10 | static class ErrorExtensions 11 | { 12 | public static ParserResult ToParserResult(this IEnumerable errors, T instance) 13 | { 14 | return errors.Any() 15 | ? (ParserResult)new NotParsed(instance.GetType().ToTypeInfo(), errors) 16 | : (ParserResult)new Parsed(instance); 17 | } 18 | 19 | public static IEnumerable OnlyMeaningfulOnes(this IEnumerable errors) 20 | { 21 | return errors 22 | .Where(e => !e.StopsProcessing) 23 | .Where(e => !(e.Tag == ErrorType.UnknownOptionError 24 | && ((UnknownOptionError)e).Token.EqualsOrdinalIgnoreCase("help"))); 25 | } 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/CommandLine/HelpTextExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | using System; 3 | using System.IO; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace CommandLine 8 | { 9 | public static class HelpTextExtensions 10 | { 11 | /// 12 | /// return true when errors contain HelpXXXError 13 | /// 14 | public static bool IsHelp(this IEnumerable errs) 15 | { 16 | if (errs.Any(x => x.Tag == ErrorType.HelpRequestedError || 17 | x.Tag == ErrorType.HelpVerbRequestedError)) 18 | return true; 19 | //when AutoHelp=false in parser, help is disabled and Parser raise UnknownOptionError 20 | return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "help"); 21 | } 22 | 23 | /// 24 | /// return true when errors contain VersionXXXError 25 | /// 26 | public static bool IsVersion(this IEnumerable errs) 27 | { 28 | if (errs.Any(x => x.Tag == ErrorType.VersionRequestedError)) 29 | return true; 30 | //when AutoVersion=false in parser, Version is disabled and Parser raise UnknownOptionError 31 | return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "version"); 32 | } 33 | /// 34 | /// redirect errs to Console.Error, and to Console.Out for help/version error 35 | /// 36 | public static TextWriter Output(this IEnumerable errs) 37 | { 38 | if (errs.IsHelp() || errs.IsVersion()) 39 | return Console.Out; 40 | return Console.Error; 41 | } 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/EnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace CommandLine.Infrastructure 8 | { 9 | static class EnumerableExtensions 10 | { 11 | public static int IndexOf(this IEnumerable source, Func predicate) 12 | { 13 | var index = -1; 14 | foreach (var item in source) 15 | { 16 | index++; 17 | if (predicate(item)) 18 | { 19 | break; 20 | } 21 | } 22 | return index; 23 | } 24 | 25 | public static object ToUntypedArray(this IEnumerable value, Type type) 26 | { 27 | var array = Array.CreateInstance(type, value.Count()); 28 | value.ToArray().CopyTo(array, 0); 29 | return array; 30 | } 31 | 32 | public static bool Empty(this IEnumerable source) 33 | { 34 | return !source.Any(); 35 | } 36 | 37 | /// 38 | /// Breaks a collection into groups of a specified size. 39 | /// 40 | /// A collection of . 41 | /// The number of items each group shall contain. 42 | /// An enumeration of T[]. 43 | /// An incomplete group at the end of the source collection will be silently dropped. 44 | public static IEnumerable Group(this IEnumerable source, int groupSize) 45 | { 46 | if (groupSize < 1) 47 | { 48 | throw new ArgumentOutOfRangeException(nameof(groupSize)); 49 | } 50 | 51 | T[] group = new T[groupSize]; 52 | int groupIndex = 0; 53 | 54 | foreach (var item in source) 55 | { 56 | group[groupIndex++] = item; 57 | 58 | if (groupIndex == groupSize) 59 | { 60 | yield return group; 61 | 62 | group = new T[groupSize]; 63 | groupIndex = 0; 64 | } 65 | } 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/ExceptionExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace CommandLine.Infrastructure 8 | { 9 | static class ExceptionExtensions 10 | { 11 | public static void RethrowWhenAbsentIn(this Exception exception, IEnumerable validExceptions) 12 | { 13 | if (!validExceptions.Contains(exception.GetType())) 14 | { 15 | throw exception; 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/FSharpOptionHelper.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using CommandLine.Core; 5 | using Microsoft.FSharp.Core; 6 | 7 | namespace CommandLine.Infrastructure 8 | { 9 | static class FSharpOptionHelper 10 | { 11 | public static Type GetUnderlyingType(Type type) 12 | { 13 | return type 14 | .GetGenericArguments()[0]; 15 | } 16 | 17 | public static object Some(Type type, object value) 18 | { 19 | return typeof(FSharpOption<>) 20 | .MakeGenericType(type) 21 | .StaticMethod( 22 | "Some", value); 23 | } 24 | 25 | public static object None(Type type) 26 | { 27 | return typeof(FSharpOption<>) 28 | .MakeGenericType(type) 29 | .StaticProperty( 30 | "None"); 31 | } 32 | 33 | public static object ValueOf(object value) 34 | { 35 | return typeof(FSharpOption<>) 36 | .MakeGenericType(GetUnderlyingType(value.GetType())) 37 | .InstanceProperty( 38 | "Value", value); 39 | } 40 | 41 | public static bool IsSome(object value) 42 | { 43 | return (bool)typeof(FSharpOption<>) 44 | .MakeGenericType(GetUnderlyingType(value.GetType())) 45 | .StaticMethod( 46 | "get_IsSome", value); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/LocalizableAttributeProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace CommandLine.Infrastructure 5 | { 6 | internal class LocalizableAttributeProperty 7 | { 8 | private string _propertyName; 9 | private string _value; 10 | private Type _type; 11 | private PropertyInfo _localizationPropertyInfo; 12 | 13 | public LocalizableAttributeProperty(string propertyName) 14 | { 15 | _propertyName = propertyName; 16 | } 17 | 18 | public string Value 19 | { 20 | get { return GetLocalizedValue(); } 21 | set 22 | { 23 | _localizationPropertyInfo = null; 24 | _value = value; 25 | } 26 | } 27 | 28 | public Type ResourceType 29 | { 30 | set 31 | { 32 | _localizationPropertyInfo = null; 33 | _type = value; 34 | } 35 | } 36 | 37 | private string GetLocalizedValue() 38 | { 39 | if (String.IsNullOrEmpty(_value) || _type == null) 40 | return _value; 41 | if (_localizationPropertyInfo == null) 42 | { 43 | // Static class IsAbstract 44 | if (!_type.IsVisible) 45 | throw new ArgumentException($"Invalid resource type '{_type.FullName}'! {_type.Name} is not visible for the parser! Change resources 'Access Modifier' to 'Public'", _propertyName); 46 | PropertyInfo propertyInfo = _type.GetProperty(_value, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static); 47 | if (propertyInfo == null || !propertyInfo.CanRead || (propertyInfo.PropertyType != typeof(string) && !propertyInfo.PropertyType.CanCast())) 48 | throw new ArgumentException($"Invalid resource property name! Localized value: {_value}", _propertyName); 49 | _localizationPropertyInfo = propertyInfo; 50 | } 51 | 52 | return _localizationPropertyInfo.GetValue(null, null).Cast(); 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/PopsicleSetter.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Infrastructure 6 | { 7 | static class PopsicleSetter 8 | { 9 | public static void Set(bool consumed, ref T field, T value) 10 | { 11 | if (consumed) 12 | { 13 | throw new InvalidOperationException(); 14 | } 15 | 16 | field = value; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/CommandLine/Infrastructure/ReferenceEqualityComparer.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using System.Runtime.CompilerServices; 6 | 7 | namespace CommandLine.Infrastructure 8 | { 9 | internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer 10 | { 11 | public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer(); 12 | 13 | public new bool Equals(object x, object y) 14 | { 15 | return ReferenceEquals(x, y); 16 | } 17 | 18 | public int GetHashCode(object obj) 19 | { 20 | return RuntimeHelpers.GetHashCode(obj); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/CommandLine/IntrospectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CommandLine 8 | { 9 | #if NET40 10 | 11 | internal static class IntrospectionExtensions 12 | { 13 | public static Type GetTypeInfo(this Type type) 14 | { 15 | return type; 16 | } 17 | } 18 | #endif 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/CommandLine/NullInstance.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine 4 | { 5 | /// 6 | /// Models a null result when constructing a in a faling verbs scenario. 7 | /// 8 | public sealed class NullInstance 9 | { 10 | internal NullInstance() { } 11 | } 12 | } -------------------------------------------------------------------------------- /src/CommandLine/ParserResultExtensionsAsync.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Threading.Tasks; 5 | 6 | namespace CommandLine 7 | { 8 | public static partial class ParserResultExtensions 9 | { 10 | #if !NET40 11 | /// 12 | /// Executes asynchronously if contains 13 | /// parsed values. 14 | /// 15 | /// Type of the target instance built with parsed value. 16 | /// An instance. 17 | /// The to execute. 18 | /// The same instance as a instance. 19 | public static async Task> WithParsedAsync(this ParserResult result, Func action) 20 | { 21 | if (result is Parsed parsed) 22 | { 23 | await action(parsed.Value); 24 | } 25 | return result; 26 | } 27 | 28 | /// 29 | /// Executes asynchronously if parsed values are of . 30 | /// 31 | /// Type of the target instance built with parsed value. 32 | /// An verb result instance. 33 | /// The to execute. 34 | /// The same instance as a instance. 35 | public static async Task> WithParsedAsync(this ParserResult result, Func action) 36 | { 37 | if (result is Parsed parsed) 38 | { 39 | if (parsed.Value is T value) 40 | { 41 | await action(value); 42 | } 43 | } 44 | return result; 45 | } 46 | 47 | /// 48 | /// Executes asynchronously if lacks 49 | /// parsed values and contains errors. 50 | /// 51 | /// Type of the target instance built with parsed value. 52 | /// An instance. 53 | /// The delegate to execute. 54 | /// The same instance as a instance. 55 | public static async Task> WithNotParsedAsync(this ParserResult result, Func, Task> action) 56 | { 57 | if (result is NotParsed notParsed) 58 | { 59 | await action(notParsed.Errors); 60 | } 61 | return result; 62 | } 63 | #endif 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/CommandLine/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Runtime.CompilerServices; 4 | 5 | [assembly: InternalsVisibleTo("CommandLine.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010009ab24ef889cd26bf46f7eaeda28e0fa5c04c50c93c6e121337b154bca0a1fd58ac6cb86195b709c2120f482730ced04a0e167a5758e56d3464bfabafe022b31510c39a61968fde795480dd60f6a396015c5f69a942074a3f4654b6dd66d0c63608bea78bdf96b35b1b48bb75741c2caad1f70579f286f1dbc2c560511c648d2")] 6 | -------------------------------------------------------------------------------- /src/CommandLine/Text/AssemblyLicenseAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace CommandLine.Text 7 | { 8 | /// 9 | /// Models a multiline assembly license text. 10 | /// 11 | [AttributeUsage(AttributeTargets.Assembly, Inherited = false), ComVisible(false)] 12 | public sealed class AssemblyLicenseAttribute : MultilineTextAttribute 13 | { 14 | /// 15 | /// Initializes a new instance of the class 16 | /// with one line of text. 17 | /// 18 | /// First line of license text. 19 | public AssemblyLicenseAttribute(string line1) 20 | : base(line1) 21 | { 22 | } 23 | 24 | /// 25 | /// Initializes a new instance of the class 26 | /// with two lines of text. 27 | /// 28 | /// First line of license text. 29 | /// Second line of license text. 30 | public AssemblyLicenseAttribute(string line1, string line2) 31 | : base(line1, line2) 32 | { 33 | } 34 | 35 | /// 36 | /// Initializes a new instance of the class 37 | /// with three lines of text. 38 | /// 39 | /// First line of license text. 40 | /// Second line of license text. 41 | /// Third line of license text. 42 | public AssemblyLicenseAttribute(string line1, string line2, string line3) 43 | : base(line1, line2, line3) 44 | { 45 | } 46 | 47 | /// 48 | /// Initializes a new instance of the class 49 | /// with four lines of text. 50 | /// 51 | /// First line of license text. 52 | /// Second line of license text. 53 | /// Third line of license text. 54 | /// Fourth line of license text. 55 | public AssemblyLicenseAttribute(string line1, string line2, string line3, string line4) 56 | : base(line1, line2, line3, line4) 57 | { 58 | } 59 | 60 | /// 61 | /// Initializes a new instance of the class 62 | /// with five lines of text. 63 | /// 64 | /// First line of license text. 65 | /// Second line of license text. 66 | /// Third line of license text. 67 | /// Fourth line of license text. 68 | /// Fifth line of license text. 69 | public AssemblyLicenseAttribute(string line1, string line2, string line3, string line4, string line5) 70 | : base(line1, line2, line3, line4, line5) 71 | { 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/CommandLine/Text/AssemblyUsageAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace CommandLine.Text 7 | { 8 | /// 9 | /// Models a multiline assembly usage text. 10 | /// 11 | [AttributeUsage(AttributeTargets.Assembly, Inherited = false), ComVisible(false)] 12 | public sealed class AssemblyUsageAttribute : MultilineTextAttribute 13 | { 14 | /// 15 | /// Initializes a new instance of the class 16 | /// with one line of text. 17 | /// 18 | /// First line of usage text. 19 | public AssemblyUsageAttribute(string line1) 20 | : base(line1) 21 | { 22 | } 23 | 24 | /// 25 | /// Initializes a new instance of the class 26 | /// with two lines of text. 27 | /// 28 | /// First line of usage text. 29 | /// Second line of usage text. 30 | public AssemblyUsageAttribute(string line1, string line2) 31 | : base(line1, line2) 32 | { 33 | } 34 | 35 | /// 36 | /// Initializes a new instance of the class 37 | /// with three lines of text. 38 | /// 39 | /// First line of usage text. 40 | /// Second line of usage text. 41 | /// Third line of usage text. 42 | public AssemblyUsageAttribute(string line1, string line2, string line3) 43 | : base(line1, line2, line3) 44 | { 45 | } 46 | 47 | /// 48 | /// Initializes a new instance of the class 49 | /// with four lines of text. 50 | /// 51 | /// First line of usage text. 52 | /// Second line of usage text. 53 | /// Third line of usage text. 54 | /// Fourth line of usage text. 55 | public AssemblyUsageAttribute(string line1, string line2, string line3, string line4) 56 | : base(line1, line2, line3, line4) 57 | { 58 | } 59 | 60 | /// 61 | /// Initializes a new instance of the class 62 | /// with five lines of text. 63 | /// 64 | /// First line of usage text. 65 | /// Second line of usage text. 66 | /// Third line of usage text. 67 | /// Fourth line of usage text. 68 | /// Fifth line of usage text. 69 | public AssemblyUsageAttribute(string line1, string line2, string line3, string line4, string line5) 70 | : base(line1, line2, line3, line4, line5) 71 | { 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/CommandLine/Text/UsageAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Text 6 | { 7 | /// 8 | /// Applied to a static property that yields a sequence of , 9 | /// provides data to render usage section of help screen. 10 | /// 11 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 12 | public sealed class UsageAttribute : Attribute 13 | { 14 | /// 15 | /// Application name, script or any means that starts current program. 16 | /// 17 | public string ApplicationAlias { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/CommandLine/ValueAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine 6 | { 7 | /// 8 | /// Models an value specification, or better how to handle values not bound to options. 9 | /// 10 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 11 | public sealed class ValueAttribute : BaseAttribute 12 | { 13 | private readonly int index; 14 | private string metaName; 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | public ValueAttribute(int index) : base() 20 | { 21 | this.index = index; 22 | this.metaName = string.Empty; 23 | } 24 | 25 | /// 26 | /// Gets the position this option has on the command line. 27 | /// 28 | public int Index 29 | { 30 | get { return index; } 31 | } 32 | 33 | /// 34 | /// Gets or sets name of this positional value specification. 35 | /// 36 | public string MetaName 37 | { 38 | get { return metaName; } 39 | set 40 | { 41 | metaName = value ?? throw new ArgumentNullException("value"); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /src/CommandLine/VerbAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace CommandLine 7 | { 8 | /// 9 | /// Models a verb command specification. 10 | /// 11 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] 12 | //public sealed class VerbAttribute : Attribute 13 | public class VerbAttribute : Attribute 14 | { 15 | private readonly Infrastructure.LocalizableAttributeProperty helpText; 16 | private Type resourceType; 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// The long name of the verb command. 22 | /// Whether the verb is the default verb. 23 | /// aliases for this verb. i.e. "move" and "mv" 24 | /// Thrown if is null, empty or whitespace and is false. 25 | public VerbAttribute(string name, bool isDefault = false, string[] aliases = null) 26 | { 27 | if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("name"); 28 | 29 | Name = name; 30 | IsDefault = isDefault; 31 | helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText)); 32 | resourceType = null; 33 | Aliases = aliases ?? new string[0]; 34 | } 35 | 36 | /// 37 | /// Gets the verb name. 38 | /// 39 | public string Name { get; private set; } 40 | 41 | /// 42 | /// Gets or sets a value indicating whether a command line verb is visible in the help text. 43 | /// 44 | public bool Hidden 45 | { 46 | get; 47 | set; 48 | } 49 | 50 | /// 51 | /// Gets or sets a short description of this command line option. Usually a sentence summary. 52 | /// 53 | public string HelpText 54 | { 55 | get => helpText.Value ?? string.Empty; 56 | set => helpText.Value = value ?? throw new ArgumentNullException("value"); 57 | } 58 | /// 59 | /// Gets or sets the that contains the resources for . 60 | /// 61 | public Type ResourceType 62 | { 63 | get => resourceType; 64 | set => resourceType = helpText.ResourceType = value; 65 | } 66 | 67 | /// 68 | /// Gets whether this verb is the default verb. 69 | /// 70 | public bool IsDefault { get; private set; } 71 | 72 | /// 73 | /// Gets or sets the aliases 74 | /// 75 | public string[] Aliases { get; private set; } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/CommandLine.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Library 5 | net461;netcoreapp3.1 6 | $(DefineConstants);SKIP_FSHARP 7 | ..\..\CommandLine.snk 8 | true 9 | gsscoder;nemec;ericnewton76 10 | Command Line Parser Library 11 | $(VersionSuffix) 12 | 2.5.0 13 | Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors 14 | true 15 | 16 | 17 | 18 | 19 | 20 | 21 | $(DefineConstants);PLATFORM_DOTNET 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/CultureInfoExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Globalization; 5 | using System.Threading; 6 | 7 | namespace CommandLine.Tests 8 | { 9 | struct CultureHandlers 10 | { 11 | public Action ChangeCulture; 12 | public Action ResetCulture; 13 | } 14 | 15 | static class CultureInfoExtensions 16 | { 17 | public static CultureHandlers MakeCultureHandlers(this CultureInfo newCulture) 18 | { 19 | var currentCulutre = Thread.CurrentThread.CurrentCulture; 20 | 21 | Action changer = () => Thread.CurrentThread.CurrentCulture = newCulture; 22 | 23 | Action resetter = () => Thread.CurrentThread.CurrentCulture = currentCulutre; 24 | 25 | return new CultureHandlers { ChangeCulture = changer, ResetCulture = resetter }; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/CustomAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | [AttributeUsage(AttributeTargets.All)] 4 | class CustomAttribute: Attribute {} -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Custom_Struct.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class CustomStructOptions 8 | { 9 | [Option('c', "custom", HelpText = "Custom Type")] 10 | public CustomStruct Custom { get; set; } 11 | } 12 | 13 | public struct CustomStruct 14 | { 15 | public string Input { get; set; } 16 | public string Server { get; set; } 17 | public int Port { get; set; } 18 | public CustomStruct(string url) 19 | { 20 | Input = url; 21 | Server = ""; 22 | Port = 80; 23 | var data = url.Split(':'); 24 | if (data.Length == 2) 25 | { 26 | Server = data[0]; 27 | Port = Convert.ToInt32(data[1]); 28 | } 29 | } 30 | } 31 | 32 | public class CustomClassOptions 33 | { 34 | [Option('c', "custom", HelpText = "Custom Type")] 35 | public CustomClass Custom { get; set; } 36 | } 37 | 38 | public class CustomClass 39 | { 40 | public string Input { get; set; } 41 | public string Server { get; set; } 42 | public int Port { get; set; } 43 | public CustomClass(string url) 44 | { 45 | Input = url; 46 | Server = ""; 47 | Port = 80; 48 | var data = url.Split(':'); 49 | if (data.Length == 2) 50 | { 51 | Server = data[0]; 52 | Port = Convert.ToInt32(data[1]); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/HelpTextWithLineBreaksAndSubIndentation_Options.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class HelpTextWithLineBreaksAndSubIndentation_Options 4 | { 5 | 6 | [Option(HelpText = @"This is a help text description where we want: 7 | * The left pad after a linebreak to be honoured and the indentation to be preserved across to the next line 8 | * The ability to return to no indent. 9 | Like this.")] 10 | public string StringValue { get; set; } 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/HelpTextWithLineBreaks_Options.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class HelpTextWithLineBreaks_Options 4 | { 5 | [Option(HelpText = 6 | @"This is a help text description. 7 | It has multiple lines. 8 | We also want to ensure that indentation is correct.")] 9 | public string StringValue { get; set; } 10 | 11 | 12 | [Option(HelpText = @"This is a help text description where we want 13 | the left pad after a linebreak to be honoured so that 14 | we can sub-indent within a description.")] 15 | public string StringValu2 { get; set; } 16 | 17 | 18 | [Option(HelpText = @"This is a help text description where we want 19 | The left pad after a linebreak to be honoured and the indentation to be preserved across to the next line in a way that looks pleasing")] 20 | public string StringValu3 { get; set; } 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/HelpTextWithMixedLineBreaks_Options.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class HelpTextWithMixedLineBreaks_Options 4 | { 5 | [Option(HelpText = 6 | "This is a help text description\n It has multiple lines.\r\n Third line")] 7 | public string StringValue { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Hidden_Option.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Hidden_Option 4 | { 5 | [Option('h', "hiddenOption", Hidden = true)] 6 | public string HiddenOption { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/IInterface_With_Two_Scalar_Options.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public interface IInterface_With_Two_Scalar_Options 4 | { 5 | [Option('v', "verbose", HelpText = "Comment extensively every operation.")] 6 | bool Verbose { get; set; } 7 | 8 | [Option(HelpText = "Input file.")] 9 | string InputFile { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Immutable_Simple_Options.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Immutable_Simple_Options 8 | { 9 | private readonly string stringValue; 10 | private readonly IEnumerable intSequence; 11 | private readonly bool boolValue; 12 | private readonly long longValue; 13 | 14 | public Immutable_Simple_Options(string stringValue, IEnumerable intSequence, bool boolValue, long longValue) 15 | { 16 | this.stringValue = stringValue; 17 | this.intSequence = intSequence; 18 | this.boolValue = boolValue; 19 | this.longValue = longValue; 20 | } 21 | 22 | [Option(HelpText = "Define a string value here.")] 23 | public string StringValue { get { return stringValue; } } 24 | 25 | [Option('i', Min = 3, Max = 4, HelpText = "Define a int sequence here.")] 26 | public IEnumerable IntSequence { get { return intSequence; } } 27 | 28 | [Option('x', HelpText = "Define a boolean or switch value here.")] 29 | public bool BoolValue { get { return boolValue; } } 30 | 31 | [Value(0)] 32 | public long LongValue { get { return longValue; } } 33 | } 34 | 35 | public class Immutable_Simple_Options_Invalid_Ctor_Args 36 | { 37 | private readonly string stringValue; 38 | private readonly IEnumerable intSequence; 39 | private readonly bool boolValue; 40 | private readonly long longValue; 41 | 42 | public Immutable_Simple_Options_Invalid_Ctor_Args(string stringValue1, IEnumerable intSequence2, bool boolValue, long longValue) 43 | { 44 | this.stringValue = stringValue1; 45 | this.intSequence = intSequence2; 46 | this.boolValue = boolValue; 47 | this.longValue = longValue; 48 | } 49 | 50 | [Option(HelpText = "Define a string value here.")] 51 | public string StringValue { get { return stringValue; } } 52 | 53 | [Option('i', Min = 3, Max = 4, HelpText = "Define a int sequence here.")] 54 | public IEnumerable IntSequence { get { return intSequence; } } 55 | 56 | [Option('x', HelpText = "Define a boolean or switch value here.")] 57 | public bool BoolValue { get { return boolValue; } } 58 | 59 | [Value(0)] 60 | public long LongValue { get { return longValue; } } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Immutable_Verb_Fakes.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | [Verb("add", HelpText = "Add file contents to the index.")] 8 | class Immutable_Add_Verb 9 | { 10 | private readonly bool patch; 11 | private readonly bool force; 12 | private readonly string fileName; 13 | 14 | public Immutable_Add_Verb(bool patch, bool force, string fileName) 15 | { 16 | this.patch = patch; 17 | this.force = force; 18 | this.fileName = fileName; 19 | } 20 | 21 | [Option('p', "patch", SetName = "mode", 22 | HelpText = "Interactively choose hunks of patch between the index and the work tree and add them to the index.")] 23 | public bool Patch { get { return patch; } } 24 | 25 | [Option('f', "force", SetName = "mode", 26 | HelpText = "Allow adding otherwise ignored files.")] 27 | public bool Force { get { return force; } } 28 | 29 | [Value(0)] 30 | public string FileName { get { return fileName; } } 31 | } 32 | 33 | [Verb("commit", HelpText = "Record changes to the repository.")] 34 | class Immutable_Commit_Verb 35 | { 36 | private readonly bool patch; 37 | private readonly bool amend; 38 | 39 | public Immutable_Commit_Verb(bool patch, bool amend) 40 | { 41 | this.patch = patch; 42 | this.amend = amend; 43 | } 44 | 45 | [Option('p', "patch", 46 | HelpText = "Use the interactive patch selection interface to chose which changes to commit.")] 47 | public bool Patch { get { return patch; } } 48 | 49 | [Option("amend", HelpText = "Used to amend the tip of the current branch.")] 50 | public bool Amend { get { return amend; } } 51 | } 52 | 53 | [Verb("clone", HelpText = "Clone a repository into a new directory.")] 54 | class Immutable_Clone_Verb 55 | { 56 | private readonly bool noHardLinks; 57 | private readonly bool quiet; 58 | private readonly IEnumerable urls; 59 | 60 | public Immutable_Clone_Verb(bool noHardLinks, bool quiet, IEnumerable urls) 61 | { 62 | this.noHardLinks = noHardLinks; 63 | this.quiet = quiet; 64 | this.urls = urls; 65 | } 66 | 67 | [Option("no-hardlinks", 68 | HelpText = "Optimize the cloning process from a repository on a local filesystem by copying files.")] 69 | public bool NoHardLinks { get { return noHardLinks; } } 70 | 71 | [Option('q', "quiet", 72 | HelpText = "Suppress summary message.")] 73 | public bool Quiet { get { return quiet; } } 74 | 75 | [Value(0)] 76 | public IEnumerable Urls { get { return urls; } } 77 | } 78 | } -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Mutable_Without_Empty_Constructor.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Mutable_Without_Empty_Constructor 6 | { 7 | [Option("amend", HelpText = "Used to amend the tip of the current branch.")] 8 | public bool Amend { get; set; } 9 | 10 | private Mutable_Without_Empty_Constructor() 11 | { 12 | } 13 | 14 | public static Mutable_Without_Empty_Constructor Create() 15 | { 16 | return new Mutable_Without_Empty_Constructor(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_HelpText_Ordering.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | 6 | [Verb("verb1")] 7 | class Options_HelpText_Ordering_Verb1 8 | { 9 | [Option('a', "alpha", Required = true)] 10 | public string alphaOption { get; set; } 11 | 12 | [Option('b', "alpha2", Required = true)] 13 | public string alphaTwoOption { get; set; } 14 | 15 | [Option('d', "charlie", Required = false)] 16 | public string deltaOption { get; set; } 17 | 18 | [Option('c', "bravo", Required = false)] 19 | public string charlieOption { get; set; } 20 | 21 | [Option('f', "foxtrot", Required = false)] 22 | public string foxOption { get; set; } 23 | 24 | [Option('e', "echo", Required = false)] 25 | public string echoOption { get; set; } 26 | 27 | [Value(0)] public string someExtraOption { get; set; } 28 | } 29 | 30 | [Verb("verb2")] 31 | class Options_HelpText_Ordering_Verb2 32 | { 33 | [Option('a', "alpha", Required = true)] 34 | public string alphaOption { get; set; } 35 | 36 | [Option('b', "alpha2", Required = true)] 37 | public string alphaTwoOption { get; set; } 38 | 39 | [Option('c', "bravo", Required = false)] 40 | public string charlieOption { get; set; } 41 | 42 | [Option('d', "charlie", Required = false)] 43 | public string deltaOption { get; set; } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Both_Min_And_Max_Set_To_Zero.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Both_Min_And_Max_Set_To_Zero 8 | { 9 | [Option(Min=0, Max=0)] 10 | public IEnumerable BadDoubleSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Custom_Help_Option.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_Custom_Help_Option : Simple_Options 4 | { 5 | [Option('h', "help")] 6 | public bool Help { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Custom_Version_Option.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_Custom_Version_Option : Simple_Options 4 | { 5 | [Option('v', "version")] 6 | public bool MyVersion { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Default_Set_To_Sequence.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Default_Set_To_Sequence 8 | { 9 | [Option('z', "strseq", Default = new[] { "a", "b", "c" })] 10 | public IEnumerable StringSequence { get; set; } 11 | 12 | [Option('y', "intseq", Default = new[] { 1, 2, 3 })] 13 | public IEnumerable IntSequence { get; set; } 14 | 15 | [Option('q', "dblseq", Default = new[] { 1.1, 2.2, 3.3 })] 16 | public IEnumerable DoubleSequence { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Defaults.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_Defaults 4 | { 5 | [Option(Default = 99)] 6 | public int P1 { get; set; } 7 | [Option()] 8 | public string P2 { get; set; } 9 | [Option(Default = 88)] 10 | public int P3 { get; set; } 11 | [Option(Default = Shapes.Square)] 12 | public Shapes P4 { get; set; } 13 | } 14 | class Nuulable_Options_With_Defaults 15 | { 16 | [Option(Default = 99)] 17 | public int? P1 { get; set; } 18 | [Option()] 19 | public string P2 { get; set; } 20 | [Option(Default = 88)] 21 | public int? P3 { get; set; } 22 | [Option(Default = Shapes.Square)] 23 | public Shapes? P4 { get; set; } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Enum_Having_HelpText.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public enum Shapes 6 | { 7 | Circle, 8 | Square, 9 | Triangle 10 | } 11 | 12 | class Options_With_Enum_Having_HelpText 13 | { 14 | [Option(HelpText = "Define a string value here.")] 15 | public string StringValue { get; set; } 16 | 17 | [Option(HelpText="Define a enum value here.")] 18 | public Shapes Shape { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_FSharpOption.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using Microsoft.FSharp.Core; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_FSharpOption 8 | { 9 | [Option] 10 | public FSharpOption FileName { get; set; } 11 | 12 | [Value(0)] 13 | public FSharpOption Offset { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_FileDirectoryInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.IO; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_FileDirectoryInfo 8 | { 9 | [Option('s', "stringPath")] 10 | public string StringPath { get; set; } 11 | 12 | [Option('f', "filePath")] 13 | public FileInfo FilePath { get; set; } 14 | 15 | [Option('d', "directoryPath")] 16 | public DirectoryInfo DirectoryPath { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_FlagCounter_Switches.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Options_With_FlagCounter_Switches 6 | { 7 | [Option('v', FlagCounter=true)] 8 | public int Verbose { get; set; } 9 | 10 | [Option('s', FlagCounter=true)] 11 | public int Silent { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Group.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Options_With_Group 4 | { 5 | [Option('v', "version")] 6 | public string Version { get; set; } 7 | 8 | [Option("option1", Group = "err-group")] 9 | public string Option1 { get; set; } 10 | 11 | [Option("option2", Group = "err-group")] 12 | public string Option2 { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Guid.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_Guid 8 | { 9 | [Option('t', "txid")] 10 | public Guid TransactionId { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_HelpText_And_MetaValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_HelpText_And_MetaValue 8 | { 9 | [Option( 10 | MetaValue = "STR", 11 | HelpText = "Define a string value here.")] 12 | public string StringValue { get; set; } 13 | 14 | [Option('i', Min = 3, Max = 4, 15 | MetaValue = "INTSEQ", 16 | HelpText = "Define a int sequence here.")] 17 | public IEnumerable IntSequence { get; set; } 18 | 19 | [Option('x', 20 | HelpText = "Define a boolean or switch value here.")] 21 | public bool BoolValue { get; set; } 22 | 23 | [Value(0, 24 | MetaName = "number", 25 | MetaValue = "NUM", 26 | HelpText = "Define a long value here.")] 27 | public long LongValue { get; set; } 28 | 29 | [Value(1, 30 | MetaName = "paintcolor", 31 | MetaValue = "COLOR", 32 | HelpText = "Define a color value here.")] 33 | public Colors ColorValue { get; set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Interface.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_Interface : IInterface_With_Two_Scalar_Options 4 | { 5 | public bool Verbose { get; set; } 6 | 7 | public string InputFile { get; set; } 8 | 9 | [Option(HelpText = "Output file.")] 10 | public string OutputFile { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_InvalidDefaults.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace CommandLine.Tests.Fakes 3 | { 4 | class Options_With_InvalidDefaults 5 | { 6 | // Default of string and integer type property will also throw. 7 | 8 | [Option(Default = false)] 9 | public string FileName { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Max_Set_To_Zero.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Max_Set_To_Zero 8 | { 9 | [Option(Max=0)] 10 | public IEnumerable BadStringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_MetaValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_MetaValue 6 | { 7 | [Option('v', "verbose", HelpText = "Comment extensively every operation.")] 8 | public bool Verbose { get; set; } 9 | 10 | [Option('i', "input-file", MetaValue = "FILE", Required = true, HelpText = "Specify input FILE to be processed.")] 11 | public string FileName { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Min_Set_To_Zero.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Min_Set_To_Zero 8 | { 9 | [Option(Min=0)] 10 | public IEnumerable BadIntSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Multiple_Groups.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Options_With_Multiple_Groups 4 | { 5 | [Option('v', "version")] 6 | public string Version { get; set; } 7 | 8 | [Option("option11", Group = "err-group")] 9 | public string Option11 { get; set; } 10 | 11 | [Option("option12", Group = "err-group")] 12 | public string Option12 { get; set; } 13 | 14 | [Option("option21", Group = "err-group2")] 15 | public string Option21 { get; set; } 16 | 17 | [Option("option22", Group = "err-group2")] 18 | public string Option22 { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Named_And_Empty_Sets.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Named_And_Empty_Sets 6 | { 7 | [Option(SetName = "web")] 8 | public string WebUrl { get; set; } 9 | 10 | [Option(SetName = "web")] 11 | public int MaxLinks { get; set; } 12 | 13 | [Option(SetName = "ftp")] 14 | public string FtpUrl { get; set; } 15 | 16 | [Option(SetName = "ftp")] 17 | public int MaxFiles { get; set; } 18 | 19 | [Option] 20 | public bool Verbose { get; set; } 21 | 22 | [Option] 23 | public bool Interactive { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Nullable_Enum_Having_HelpText.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Nullable_Enum_Having_HelpText 6 | { 7 | [Option(HelpText = "Define a string value here.")] 8 | public string StringValue { get; set; } 9 | 10 | [Option(HelpText="Define a enum value here.")] 11 | public Shapes? Shape { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Nullables.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Nullables 6 | { 7 | [Option('i', "nullable-int")] 8 | public int? NullableInt { get; set; } 9 | 10 | [Value(0)] 11 | public long? NullableLong { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Only_Explicit_Interface.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_Only_Explicit_Interface : IInterface_With_Two_Scalar_Options 4 | { 5 | bool IInterface_With_Two_Scalar_Options.Verbose { get; set; } 6 | 7 | string IInterface_With_Two_Scalar_Options.InputFile { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Option_And_Value_Of_String_Type.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Linq; 5 | 6 | namespace CommandLine.Tests.Fakes 7 | { 8 | class Options_With_Option_And_Value_Of_String_Type 9 | { 10 | [Option('o', "opt")] 11 | public string OptValue { get; set; } 12 | 13 | [Value(0)] 14 | public string PosValue { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Option_Sequence_And_Value_Sequence.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Options_With_Option_Sequence_And_Value_Sequence 6 | { 7 | [Option('o', "option-seq")] 8 | public IEnumerable OptionSequence { get; set; } 9 | 10 | [Value(0)] 11 | public IEnumerable ValueSequence { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Property_Throwing_Exception.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CommandLine.Tests.Fakes 8 | { 9 | class Options_With_Property_Throwing_Exception 10 | { 11 | private string optValue; 12 | 13 | [Option('e')] 14 | public string OptValue 15 | { 16 | get 17 | { 18 | return optValue; 19 | } 20 | set 21 | { 22 | if (value != "good") 23 | throw new ArgumentException("Invalid value, only accept 'good' value"); 24 | 25 | optValue = value; 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Required_Set_To_True.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Required_Set_To_True 6 | { 7 | [Option("str", Required = true)] 8 | public string StringValue { get; set; } 9 | 10 | [Option("int")] 11 | public int IntValue { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Required_Set_To_True_For_Values.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Options_With_Required_Set_To_True_For_Values 6 | { 7 | [Value(0, Required = true)] 8 | public string StringValue { get; set; } 9 | 10 | [Value(1)] 11 | public int IntValue { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Required_Set_To_True_Within_Same_Set.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Options_With_Required_Set_To_True_Within_Same_Set { 6 | [Option("ftpurl", SetName = "SetA", Required = true)] 7 | public string FtpUrl { get; set; } 8 | 9 | [Option("weburl", SetName = "SetA", Required = true)] 10 | public string WebUrl { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Scalar_Value_And_Adjacent_SequenceString.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_Scalar_Value_And_Adjacent_SequenceString 8 | { 9 | [Value(0)] 10 | public string StringValueWithIndexZero { get; set; } 11 | 12 | [Option('s', "strseq")] 13 | public IEnumerable StringOptionSequence { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence 8 | { 9 | [Option("int-seq")] 10 | public IEnumerable IntSequence { get; set; } 11 | 12 | //[Option("string-seq")] 13 | //public IEnumerable StringSequence { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_And_Only_Max_Constraint.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_And_Only_Max_Constraint 8 | { 9 | [Option('s', "string-seq", Max=3)] 10 | public IEnumerable StringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_And_Only_Max_Constraint_For_Value.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_And_Only_Max_Constraint_For_Value 8 | { 9 | [Value(0, Max=3)] 10 | public IEnumerable StringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_And_Only_Min_Constraint.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_And_Only_Min_Constraint 8 | { 9 | [Option('s', "string-seq", Min=1)] 10 | public IEnumerable StringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_And_Only_Min_Constraint_For_Value.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_And_Only_Min_Constraint_For_Value 8 | { 9 | [Value(0, Min=1)] 10 | public IEnumerable StringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_Having_Both_Min_And_Max_Equal.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_Having_Both_Min_And_Max_Equal 8 | { 9 | [Value(0, Min=2, Max=2)] 10 | public IEnumerable StringSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_Having_Separator_And_Values.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_For_Issue_91 8 | { 9 | [Value(0, Required = true)] 10 | public string InputFileName { get; set; } 11 | 12 | [Option('o', "output")] 13 | public string OutputFileName { get; set; } 14 | 15 | [Option('i', "include", Separator = ',')] 16 | public IEnumerable Included { get; set; } 17 | 18 | [Option('e', "exclude", Separator = ',')] 19 | public IEnumerable Excluded { get; set; } 20 | } 21 | 22 | public class Options_For_Issue_454 23 | { 24 | [Option('c', "channels", Required = true, Separator = ':', HelpText = "Channel names")] 25 | public IEnumerable Channels { get; set; } 26 | 27 | [Value(0, Required = true, MetaName = "file_path", HelpText = "Path of archive to be processed")] 28 | public string ArchivePath { get; set; } 29 | } 30 | 31 | public class Options_For_Issue_510 32 | { 33 | [Option('a', "aa", Required = false, Separator = ',')] 34 | public IEnumerable A { get; set; } 35 | 36 | [Option('b', "bb", Required = false)] 37 | public string B { get; set; } 38 | 39 | [Value(0, Required = true)] 40 | public string C { get; set; } 41 | } 42 | 43 | public enum FMode { C, D, S }; 44 | 45 | public class Options_For_Issue_617 46 | { 47 | [Option("fm", Separator=',', Default = new[] { FMode.S })] 48 | public IEnumerable Mode { get; set; } 49 | 50 | [Option('q')] 51 | public bool q { get;set; } 52 | 53 | [Value(0)] 54 | public IList Files { get; set; } 55 | } 56 | 57 | public class Options_For_Issue_619 58 | { 59 | [Option("verbose", Required = false, Default = false, HelpText = "Generate process tracing information")] 60 | public bool Verbose { get; set; } 61 | 62 | [Option("outdir", Required = false, Default = ".", HelpText = "Directory to look for object file")] 63 | public string OutDir { get; set; } 64 | 65 | [Option("modules", Required = true, Separator = ',', HelpText = "Directories to look for module file")] 66 | public IEnumerable ModuleDirs { get; set; } 67 | 68 | [Option("ignore", Required = false, Separator = ' ', HelpText = "List of additional module name references to ignore")] 69 | public IEnumerable Ignores { get; set; } 70 | 71 | [Value(0, Required = true, HelpText = "List of source files to process")] 72 | public IEnumerable Srcs { get; set; } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_Having_Separator_Set.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_Having_Separator_Set 8 | { 9 | [Option("long-seq", Separator=';')] 10 | public IEnumerable LongSequence { get; set; } 11 | 12 | [Option('s', Min = 1, Max = 100, Separator = ',')] 13 | public IEnumerable StringSequence { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Sequence_Without_Range_For_Value.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Options_With_Sequence_Without_Range_For_Value 8 | { 9 | [Value(0)] 10 | public IEnumerable LongSequence { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_SetName_That_Ends_With_Previous_SetName.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | class Options_With_SetName_That_Ends_With_Previous_SetName 4 | { 5 | [Option(SetName = "web")] 6 | public string WebUrl { get; set; } 7 | 8 | [Option(SetName = "theweb")] 9 | public string SomethingElse { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Shuffled_Index_Values.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Linq; 5 | 6 | namespace CommandLine.Tests.Fakes 7 | { 8 | class Options_With_Shuffled_Index_Values 9 | { 10 | [Value(1)] 11 | public string Arg1 { get; set; } 12 | 13 | [Value(2)] 14 | public string Arg2 { get; set; } 15 | 16 | [Value(0)] 17 | public string Arg0 { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Similar_Names.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_Similar_Names 8 | { 9 | [Option("deploy", Separator = ',', HelpText= "Projects to deploy")] 10 | public IEnumerable Deploys { get; set; } 11 | 12 | [Option("profile", Required = true, HelpText = "Profile to use when restoring and publishing")] 13 | public string Profile { get; set; } 14 | 15 | [Option("configure-profile", Required = true, HelpText = "Profile to use for Configure")] 16 | public string ConfigureProfile { get; set; } 17 | } 18 | 19 | public class Options_With_Similar_Names_And_Separator 20 | { 21 | [Option('f', "flag", HelpText = "Flag")] 22 | public bool Flag { get; set; } 23 | 24 | [Option('c', "categories", Required = false, Separator = ',', HelpText = "Categories")] 25 | public IEnumerable Categories { get; set; } 26 | 27 | [Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")] 28 | public int JobId { get; set; } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Switches.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Options_With_Switches 6 | { 7 | [Option("input")] 8 | public string InputFile { get; set; } 9 | 10 | [Option('o')] 11 | public string OutputFile { get; set; } 12 | 13 | [Option('v')] 14 | public bool Verbose { get; set; } 15 | 16 | [Option('h')] 17 | public bool HumanReadable { get; set; } 18 | 19 | [Option('i')] 20 | public bool IgnoreWarnings { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_TimeSpan.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Options_With_TimeSpan 8 | { 9 | [Option('d', "duration")] 10 | public TimeSpan Duration { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Two_Option_Required_Set_To_True_And_Two_Sets.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Two_Option_Required_Set_To_True_And_Two_Sets 6 | { 7 | [Option(SetName = "web", Required = true)] 8 | public string WebUrl { get; set; } 9 | 10 | [Option(SetName = "ftp", Required = true)] 11 | public string FtpUrl { get; set; } 12 | 13 | [Option('a')] 14 | public bool FetchAsync { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Two_Options_Having_Required_Set_To_True.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Two_Options_Having_Required_Set_To_True 6 | { 7 | [Option("str", Required = true)] 8 | public string StringValue { get; set; } 9 | 10 | [Option("long", Required = true)] 11 | public long LongValue { get; set; } 12 | 13 | [Option("int")] 14 | public int IntValue { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Two_Sets.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Options_With_Two_Sets 6 | { 7 | [Option(SetName = "web")] 8 | public string WebUrl { get; set; } 9 | 10 | [Option(SetName = "web")] 11 | public int MaxLinks { get; set; } 12 | 13 | [Option(SetName = "ftp")] 14 | public string FtpUrl { get; set; } 15 | 16 | [Option(SetName = "ftp")] 17 | public int MaxFiles { get; set; } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Uri_And_SimpleType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CommandLine.Tests.Fakes 8 | { 9 | class MySimpleType 10 | { 11 | private readonly string value; 12 | 13 | public MySimpleType(string value) 14 | { 15 | this.value = value; 16 | } 17 | 18 | public string Value 19 | { 20 | get { return value; } 21 | } 22 | } 23 | 24 | class Options_With_Uri_And_SimpleType 25 | { 26 | [Option] 27 | public Uri EndPoint { get; set; } 28 | 29 | [Value(0)] 30 | public MySimpleType MyValue { get; set; } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Options_With_Value_Sequence_And_Normal_Option.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | using System.Collections.Generic; 3 | 4 | namespace CommandLine.Tests.Fakes 5 | { 6 | public class Options_With_Value_Sequence_And_Normal_Option 7 | { 8 | [Option('c', "compress", 9 | HelpText = "Compress Match Pattern, Pipe Separated (|) ", 10 | Separator = '|', 11 | Default = new[] 12 | { 13 | "*.txt", "*.log", "*.ini" 14 | })] 15 | public IEnumerable Compress { get; set; } 16 | 17 | [Value(0, 18 | HelpText = "Input Directories.", 19 | Required = true)] 20 | public IEnumerable InputDirs { get; set; } 21 | 22 | 23 | [Option('n', "name", 24 | HelpText = "Metadata Name.", 25 | Default = "WILDCARD")] 26 | public string Name { get; set; } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/ResourceFakes.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public static class StaticResource 4 | { 5 | public static string HelpText { get { return "Localized HelpText"; } } 6 | public static TypeWithImplicitCast ImplicitCastHelpText => new TypeWithImplicitCast("Localized HelpText"); 7 | public static TypeWithExplicitCast ExplicitCastHelpText => new TypeWithExplicitCast("Localized HelpText"); 8 | public static TypeWithWrongImplicitCast WrongImplicitCastHelpText => new TypeWithWrongImplicitCast(); 9 | public static TypeWithWrongExplicitCast WrongExplicitCastHelpText => new TypeWithWrongExplicitCast(); 10 | } 11 | 12 | public class NonStaticResource 13 | { 14 | public static string HelpText { get { return "Localized HelpText"; } } 15 | public static string WriteOnlyText { set { value?.ToString(); } } 16 | private static string PrivateHelpText { get { return "Localized HelpText"; } } 17 | public static TypeWithImplicitCast ImplicitCastHelpText => new TypeWithImplicitCast("Localized HelpText"); 18 | public static TypeWithExplicitCast ExplicitCastHelpText => new TypeWithExplicitCast("Localized HelpText"); 19 | public static TypeWithWrongImplicitCast WrongImplicitCastHelpText => new TypeWithWrongImplicitCast(); 20 | public static TypeWithWrongExplicitCast WrongExplicitCastHelpText => new TypeWithWrongExplicitCast(); 21 | } 22 | 23 | public class NonStaticResource_WithNonStaticProperty 24 | { 25 | public string HelpText { get { return "Localized HelpText"; } } 26 | } 27 | 28 | internal class InternalResource 29 | { 30 | public static string HelpText { get { return "Localized HelpText"; } } 31 | } 32 | 33 | public class TypeWithImplicitCast 34 | { 35 | private string value; 36 | 37 | public TypeWithImplicitCast(string value) 38 | { 39 | this.value = value; 40 | } 41 | 42 | public static implicit operator string(TypeWithImplicitCast obj) 43 | { 44 | return obj.value; 45 | } 46 | 47 | public static implicit operator int(TypeWithImplicitCast obj) 48 | { 49 | return 0; 50 | } 51 | } 52 | 53 | public class TypeWithWrongImplicitCast 54 | { 55 | public static implicit operator int(TypeWithWrongImplicitCast obj) 56 | { 57 | return 0; 58 | } 59 | } 60 | 61 | public class TypeWithExplicitCast 62 | { 63 | private string value; 64 | 65 | public TypeWithExplicitCast(string value) 66 | { 67 | this.value = value; 68 | } 69 | 70 | public static explicit operator string(TypeWithExplicitCast obj) 71 | { 72 | return obj.value; 73 | } 74 | 75 | public static explicit operator int(TypeWithExplicitCast obj) 76 | { 77 | return 0; 78 | } 79 | } 80 | 81 | public class TypeWithWrongExplicitCast 82 | { 83 | public static explicit operator int(TypeWithWrongExplicitCast obj) 84 | { 85 | return 0; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Simple_Options 8 | { 9 | [Option(HelpText = "Define a string value here.")] 10 | public string StringValue { get; set; } 11 | 12 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.")] 13 | public string ShortAndLong { get; set; } 14 | 15 | [Option('i', Min = 3, Max = 4, HelpText = "Define a int sequence here.")] 16 | public IEnumerable IntSequence { get; set; } 17 | 18 | [Option('x', HelpText = "Define a boolean or switch value here.")] 19 | public bool BoolValue { get; set; } 20 | 21 | [Value(0, HelpText = "Define a long value here.")] 22 | public long LongValue { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_Double_Value.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | class Simple_Options_With_Double_Value 6 | { 7 | [Value(0)] 8 | public double DoubleValue { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_Enum.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public enum Colors 6 | { 7 | Red, 8 | Green, 9 | Blue 10 | } 11 | 12 | class Simple_Options_With_Enum 13 | { 14 | [Option] 15 | public Colors Colors { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_ExtraArgs.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | public class Simple_Options_WithExtraArgs 8 | { 9 | [Option(HelpText = "Define a string value here.")] 10 | public string StringValue { get; set; } 11 | 12 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.")] 13 | public string ShortAndLong { get; set; } 14 | 15 | [Option('i', Min = 3, Max = 4, Separator = ',', HelpText = "Define a int sequence here.")] 16 | public IEnumerable IntSequence { get; set; } 17 | 18 | [Option('x', HelpText = "Define a boolean or switch value here.")] 19 | public bool BoolValue { get; set; } 20 | 21 | [Value(0, HelpText = "Define a long value here.")] 22 | public long LongValue { get; set; } 23 | 24 | [Value(1, HelpText = "Extra args get collected here.")] 25 | public IEnumerable ExtraArgs { get; set; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_Multiple_OptionGroups.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace CommandLine.Tests.Fakes 4 | { 5 | public class Simple_Options_With_Multiple_OptionGroups 6 | { 7 | [Option(HelpText = "Define a string value here.", Group = "string-group")] 8 | public string StringValue { get; set; } 9 | 10 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "string-group")] 11 | public string ShortAndLong { get; set; } 12 | 13 | [Option('x', HelpText = "Define a boolean or switch value here.", Group = "second-group")] 14 | public bool BoolValue { get; set; } 15 | 16 | [Option('i', Min = 3, Max = 4, HelpText = "Define a int sequence here.", Group = "second-group")] 17 | public IEnumerable IntSequence { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_OptionGroup.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Simple_Options_With_OptionGroup 4 | { 5 | [Option(HelpText = "Define a string value here.", Group = "string-group")] 6 | public string StringValue { get; set; } 7 | 8 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "string-group")] 9 | public string ShortAndLong { get; set; } 10 | 11 | [Option('x', HelpText = "Define a boolean or switch value here.")] 12 | public bool BoolValue { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_OptionGroup_MutuallyExclusiveSet.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Simple_Options_With_OptionGroup_MutuallyExclusiveSet 4 | { 5 | [Option(HelpText = "Define a string value here.", Group = "test", SetName = "setname", Default = "qwerty123")] 6 | public string StringValue { get; set; } 7 | 8 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "test", SetName = "setname")] 9 | public string ShortAndLong { get; set; } 10 | 11 | [Option('x', HelpText = "Define a boolean or switch value here.")] 12 | public bool BoolValue { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_OptionGroup_WithDefaultValue.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Simple_Options_With_OptionGroup_WithDefaultValue 4 | { 5 | [Option(HelpText = "Define a string value here.", Required = true, Group = "")] 6 | public string StringValue { get; set; } 7 | 8 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Required = true, Group = "")] 9 | public string ShortAndLong { get; set; } 10 | 11 | [Option('x', HelpText = "Define a boolean or switch value here.")] 12 | public bool BoolValue { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_OptionGroup_WithOptionDefaultValue.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Simple_Options_With_OptionGroup_WithOptionDefaultValue 4 | { 5 | [Option(HelpText = "Define a string value here.", Required = true, Group = "test", Default = "qwerty123")] 6 | public string StringValue { get; set; } 7 | 8 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Required = true, Group = "test")] 9 | public string ShortAndLong { get; set; } 10 | 11 | [Option('x', HelpText = "Define a boolean or switch value here.")] 12 | public bool BoolValue { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_Required_OptionGroup.cs: -------------------------------------------------------------------------------- 1 | namespace CommandLine.Tests.Fakes 2 | { 3 | public class Simple_Options_With_Required_OptionGroup 4 | { 5 | [Option(HelpText = "Define a string value here.", Required = true, Group = "string-group")] 6 | public string StringValue { get; set; } 7 | 8 | [Option('s', "shortandlong", HelpText = "Example with both short and long name.", Required = true, Group = "string-group")] 9 | public string ShortAndLong { get; set; } 10 | 11 | [Option('x', HelpText = "Define a boolean or switch value here.")] 12 | public bool BoolValue { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Fakes/Simple_Options_With_Values.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | 5 | namespace CommandLine.Tests.Fakes 6 | { 7 | class Simple_Options_With_Values 8 | { 9 | [Option(Default = "")] 10 | public string StringValue { get; set; } 11 | 12 | [Value(0)] 13 | public long LongValue { get; set; } 14 | 15 | [Value(1, Min = 3, Max = 3)] 16 | public IEnumerable StringSequence { get; set; } 17 | 18 | [Value(2)] 19 | public int IntValue { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace CommandLine.Tests 8 | { 9 | static class StringExtensions 10 | { 11 | public static string[] ToNotEmptyLines(this string value) 12 | { 13 | return value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); 14 | } 15 | 16 | public static string[] ToLines(this string value) 17 | { 18 | return value.Split(new[] { Environment.NewLine }, StringSplitOptions.None); 19 | } 20 | 21 | public static string[] TrimStringArray(this IEnumerable array) 22 | { 23 | return array.Select(item => item.Trim()).ToArray(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/BaseAttributeTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | namespace CommandLine.Tests.Unit 5 | { 6 | public class BaseAttributeTests 7 | { 8 | [Theory] 9 | [InlineData(null)] 10 | [InlineData(1)] 11 | public static void Default(object defaultValue) 12 | { 13 | TestBaseAttribute baseAttribute = new TestBaseAttribute(); 14 | baseAttribute.Default = defaultValue; 15 | Assert.Equal(defaultValue, baseAttribute.Default); 16 | } 17 | 18 | [Theory] 19 | [InlineData("", null, "")] 20 | [InlineData("", typeof(Fakes.StaticResource), "")] 21 | [InlineData("Help text", null, "Help text")] 22 | [InlineData("HelpText", typeof(Fakes.StaticResource), "Localized HelpText")] 23 | [InlineData("HelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")] 24 | [InlineData("ImplicitCastHelpText", typeof(Fakes.StaticResource), "Localized HelpText")] 25 | [InlineData("ImplicitCastHelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")] 26 | [InlineData("ExplicitCastHelpText", typeof(Fakes.StaticResource), "Localized HelpText")] 27 | [InlineData("ExplicitCastHelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")] 28 | public static void HelpText(string helpText, Type resourceType, string expected) 29 | { 30 | TestBaseAttribute baseAttribute = new TestBaseAttribute(); 31 | baseAttribute.HelpText = helpText; 32 | baseAttribute.ResourceType = resourceType; 33 | 34 | Assert.Equal(expected, baseAttribute.HelpText); 35 | } 36 | 37 | [Theory] 38 | [InlineData("HelpText", typeof(Fakes.NonStaticResource_WithNonStaticProperty))] 39 | [InlineData("WriteOnlyText", typeof(Fakes.NonStaticResource))] 40 | [InlineData("PrivateOnlyText", typeof(Fakes.NonStaticResource))] 41 | [InlineData("HelpText", typeof(Fakes.InternalResource))] 42 | [InlineData("WrongImplicitCastHelpText", typeof(Fakes.StaticResource))] 43 | [InlineData("WrongExplicitCastHelpText", typeof(Fakes.StaticResource))] 44 | [InlineData("WrongImplicitCastHelpText", typeof(Fakes.NonStaticResource))] 45 | [InlineData("WrongExplicitCastHelpText", typeof(Fakes.NonStaticResource))] 46 | public void ThrowsHelpText(string helpText, Type resourceType) 47 | { 48 | TestBaseAttribute baseAttribute = new TestBaseAttribute(); 49 | baseAttribute.HelpText = helpText; 50 | baseAttribute.ResourceType = resourceType; 51 | 52 | // Verify exception 53 | Assert.Throws(() => baseAttribute.HelpText.ToString()); 54 | } 55 | 56 | 57 | private class TestBaseAttribute : BaseAttribute 58 | { 59 | public TestBaseAttribute() 60 | { 61 | // Do nothing 62 | } 63 | } 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/KeyValuePairHelperTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using Xunit; 6 | using CommandLine.Core; 7 | 8 | namespace CommandLine.Tests.Unit.Core 9 | { 10 | public class KeyValuePairHelperTests 11 | { 12 | [Fact] 13 | public void Empty_token_sequence_creates_an_empty_KeyValuePair_sequence() 14 | { 15 | var expected = new KeyValuePair>[] { }; 16 | 17 | var result = KeyValuePairHelper.ForSequence(new Token[] { }); 18 | 19 | AssertEqual(expected, result); 20 | } 21 | 22 | [Fact] 23 | public void Token_sequence_creates_a_KeyValuePair_sequence() 24 | { 25 | var expected = new[] 26 | { 27 | new KeyValuePair>("seq", new[] {"seq0", "seq1", "seq2"}) 28 | }; 29 | 30 | var result = KeyValuePairHelper.ForSequence(new [] 31 | { 32 | Token.Name("seq"), Token.Value("seq0"), Token.Value("seq1"), Token.Value("seq2") 33 | }).ToArray(); 34 | 35 | AssertEqual(expected, result); 36 | } 37 | 38 | [Fact] 39 | public void Token_sequence_creates_a_KeyValuePair_sequence_for_multiple_sequences() 40 | { 41 | var expected = new[] 42 | { 43 | new KeyValuePair>("seq1", new[] {"seq10", "seq11", "seq12"}), 44 | new KeyValuePair>("seq2", new[] {"seq20", "seq21"}) 45 | }; 46 | 47 | var result = KeyValuePairHelper.ForSequence(new[] 48 | { 49 | Token.Name("seq1"), Token.Value("seq10"), Token.Value("seq11"), Token.Value("seq12"), 50 | Token.Name("seq2"), Token.Value("seq20"), Token.Value("seq21") 51 | }); 52 | 53 | AssertEqual(expected, result); 54 | } 55 | 56 | private static void AssertEqual(IEnumerable>> expected, IEnumerable>> result) 57 | { 58 | Assert.Equal(expected.Count(), result.Count()); 59 | foreach (var value in expected.Zip(result, (e, r) => new { Expected = e, Result = r })) 60 | { 61 | Assert.Equal(value.Expected.Key, value.Result.Key); 62 | Assert.Equal(value.Expected.Value, value.Result.Value); 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/NameLookupTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using Xunit; 6 | using FluentAssertions; 7 | using CommandLine.Core; 8 | using CSharpx; 9 | 10 | namespace CommandLine.Tests.Unit.Core 11 | { 12 | public class NameLookupTests 13 | { 14 | [Fact] 15 | public void Lookup_name_of_sequence_option_with_separator() 16 | { 17 | // Fixture setup 18 | var expected = Maybe.Just("."); 19 | var specs = new[] { new OptionSpecification(string.Empty, "string-seq", 20 | false, string.Empty, Maybe.Nothing(), Maybe.Nothing(), '.', null, string.Empty, string.Empty, new List(), typeof(IEnumerable), TargetType.Sequence, string.Empty)}; 21 | 22 | // Exercize system 23 | var result = NameLookup.HavingSeparator("string-seq", specs, StringComparer.Ordinal); 24 | // Verify outcome 25 | expected.Should().BeEquivalentTo(result); 26 | 27 | // Teardown 28 | } 29 | 30 | [Fact] 31 | public void Get_name_from_option_specification() 32 | { 33 | const string ShortName = "s"; 34 | const string LongName = "long"; 35 | 36 | // Fixture setup 37 | var expected = new NameInfo(ShortName, LongName); 38 | var spec = new OptionSpecification(ShortName, LongName, false, string.Empty, Maybe.Nothing(), Maybe.Nothing(), '.', null, string.Empty, string.Empty, new List(), typeof(IEnumerable), TargetType.Sequence, string.Empty); 39 | 40 | // Exercize system 41 | var result = spec.FromOptionSpecification(); 42 | 43 | // Verify outcome 44 | expected.Should().BeEquivalentTo(result); 45 | 46 | // Teardown 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/ReflectionExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using Xunit; 4 | using FluentAssertions; 5 | using CommandLine.Core; 6 | using CommandLine.Tests.Fakes; 7 | 8 | namespace CommandLine.Tests.Unit.Infrastructure 9 | { 10 | public class ReflectionHelperTests 11 | { 12 | [Fact] 13 | public static void Class_with_public_set_properties_or_fields_is_ranked_mutable() 14 | { 15 | typeof(Simple_Options).IsMutable().Should().BeTrue(); 16 | } 17 | 18 | [Fact] 19 | public static void Class_without_public_set_properties_or_fields_is_ranked_immutable() 20 | { 21 | typeof(Immutable_Simple_Options).IsMutable().Should().BeFalse(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/ScalarTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Linq; 4 | using Xunit; 5 | using FluentAssertions; 6 | using CSharpx; 7 | using CommandLine.Core; 8 | 9 | namespace CommandLine.Tests.Unit.Core 10 | { 11 | public class ScalarTests 12 | { 13 | [Fact] 14 | public void Partition_scalar_values_from_empty_token_sequence() 15 | { 16 | var expected = new Token[] { }; 17 | 18 | var tokens = TokenPartitioner.PartitionTokensByType( 19 | new Token[] { }, 20 | name => 21 | new[] { "str", "int" }.Contains(name) 22 | ? Maybe.Just(TypeDescriptor.Create(TargetType.Scalar, Maybe.Nothing())) 23 | : Maybe.Nothing()); 24 | var result = tokens.Item2; // Switch, *Scalar*, Sequence, NonOption 25 | 26 | expected.Should().BeEquivalentTo(result); 27 | } 28 | 29 | [Fact] 30 | public void Partition_scalar_values() 31 | { 32 | var expected = new [] { Token.Name("str"), Token.Value("strvalue") }; 33 | 34 | var tokens = TokenPartitioner.PartitionTokensByType( 35 | new [] 36 | { 37 | Token.Name("str"), Token.Value("strvalue"), Token.Value("freevalue"), 38 | Token.Name("x"), Token.Value("freevalue2") 39 | }, 40 | name => 41 | new[] { "str", "int" }.Contains(name) 42 | ? Maybe.Just(TypeDescriptor.Create(TargetType.Scalar, Maybe.Nothing())) 43 | : Maybe.Nothing()); 44 | var result = tokens.Item2; // Switch, *Scalar*, Sequence, NonOption 45 | 46 | expected.Should().BeEquivalentTo(result); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/SpecificationPropertyRulesTests.cs: -------------------------------------------------------------------------------- 1 | using CommandLine.Core; 2 | using CommandLine.Tests.Fakes; 3 | using CSharpx; 4 | using System.Collections.Generic; 5 | using Xunit; 6 | 7 | namespace CommandLine.Tests.Unit.Core 8 | { 9 | 10 | public class SpecificationPropertyRulesTests 11 | { 12 | [Fact] 13 | public void Lookup_allows_multi_instance() 14 | { 15 | var tokens = new[] 16 | { 17 | Token.Name("name"), 18 | Token.Value("value"), 19 | Token.Name("name"), 20 | Token.Value("value2"), 21 | }; 22 | 23 | var specProps = new[] 24 | { 25 | SpecificationProperty.Create( 26 | new OptionSpecification(string.Empty, "name", false, string.Empty, Maybe.Nothing(), Maybe.Nothing(), '\0', Maybe.Nothing(), string.Empty, string.Empty, new List(), typeof(IEnumerable), TargetType.Sequence, string.Empty), 27 | typeof(SequenceOptions).GetProperty(nameof(SequenceOptions.StringSequence)), 28 | Maybe.Just(new object())), 29 | }; 30 | 31 | var results = specProps.Validate(SpecificationPropertyRules.Lookup(tokens, true)); 32 | Assert.Empty(results); 33 | } 34 | 35 | [Fact] 36 | public void Lookup_fails_with_repeated_options_false_multi_instance() 37 | { 38 | var tokens = new[] 39 | { 40 | Token.Name("name"), 41 | Token.Value("value"), 42 | Token.Name("name"), 43 | Token.Value("value2"), 44 | }; 45 | 46 | var specProps = new[] 47 | { 48 | SpecificationProperty.Create( 49 | new OptionSpecification(string.Empty, "name", false, string.Empty, Maybe.Nothing(), Maybe.Nothing(), '\0', Maybe.Nothing(), string.Empty, string.Empty, new List(), typeof(IEnumerable), TargetType.Sequence, string.Empty), 50 | typeof(SequenceOptions).GetProperty(nameof(SequenceOptions.StringSequence)), 51 | Maybe.Just(new object())), 52 | }; 53 | 54 | var results = specProps.Validate(SpecificationPropertyRules.Lookup(tokens, false)); 55 | Assert.Contains(results, r => r.GetType() == typeof(RepeatedOptionError)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/SwitchTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Linq; 4 | using Xunit; 5 | using FluentAssertions; 6 | using CSharpx; 7 | using CommandLine.Core; 8 | 9 | namespace CommandLine.Tests.Unit.Core 10 | { 11 | public class SwitchTests 12 | { 13 | [Fact] 14 | public void Partition_switch_values_from_empty_token_sequence() 15 | { 16 | var expected = new Token[] { }; 17 | 18 | var tokens = TokenPartitioner.PartitionTokensByType( 19 | new Token[] { }, 20 | name => 21 | new[] { "x", "switch" }.Contains(name) 22 | ? Maybe.Just(TypeDescriptor.Create(TargetType.Switch, Maybe.Nothing())) 23 | : Maybe.Nothing()); 24 | var result = tokens.Item1; // *Switch*, Scalar, Sequence, NonOption 25 | 26 | expected.Should().BeEquivalentTo(result); 27 | } 28 | 29 | [Fact] 30 | public void Partition_switch_values() 31 | { 32 | var expected = new [] { Token.Name("x") }; 33 | 34 | var tokens = TokenPartitioner.PartitionTokensByType( 35 | new [] 36 | { 37 | Token.Name("str"), Token.Value("strvalue"), Token.Value("freevalue"), 38 | Token.Name("x"), Token.Value("freevalue2") 39 | }, 40 | name => 41 | new[] { "x", "switch" }.Contains(name) 42 | ? Maybe.Just(TypeDescriptor.Create(TargetType.Switch, Maybe.Nothing())) 43 | : Maybe.Nothing()); 44 | var result = tokens.Item1; // *Switch*, Scalar, Sequence, NonOption 45 | 46 | expected.Should().BeEquivalentTo(result); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Core/TokenTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using Xunit; 4 | using CommandLine.Core; 5 | 6 | namespace CommandLine.Tests.Unit.Core 7 | { 8 | public class TokenTests 9 | { 10 | [Fact] 11 | public void Equality() 12 | { 13 | Assert.True(Token.Name("nametok").Equals(Token.Name("nametok"))); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Infrastructure/FSharpOptionHelperTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. 2 | 3 | using System.Reflection; 4 | using CommandLine.Infrastructure; 5 | using CommandLine.Tests.Fakes; 6 | using Microsoft.FSharp.Core; 7 | using FluentAssertions; 8 | using Xunit; 9 | 10 | namespace CommandLine.Tests.Unit.Infrastructure 11 | { 12 | public class FSharpOptionHelperTests 13 | { 14 | [Fact] 15 | public void Match_type_returns_true_if_FSharpOption() 16 | { 17 | ReflectionHelper.IsFSharpOptionType(TestData.PropertyType) 18 | .Should().BeTrue(); 19 | } 20 | 21 | [Fact] 22 | public void Get_underlying_type() 23 | { 24 | FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType).FullName 25 | .Should().BeEquivalentTo("System.String"); 26 | } 27 | 28 | [Fact] 29 | public void Create_some() 30 | { 31 | var expected = FSharpOptionHelper.Some(FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType), "with data"); 32 | 33 | expected.Should().BeOfType>(); 34 | FSharpOption.get_IsSome((FSharpOption)expected).Should().BeTrue(); 35 | } 36 | 37 | [Fact] 38 | public void Create_none() 39 | { 40 | var expected = FSharpOptionHelper.None(FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType)); 41 | 42 | FSharpOption.get_IsNone((FSharpOption)expected).Should().BeTrue(); 43 | } 44 | 45 | private PropertyInfo TestData 46 | { 47 | get { return typeof(Options_With_FSharpOption).GetProperty("FileName", BindingFlags.Public | BindingFlags.Instance); } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/CommandLine.Tests/Unit/Issue104Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Xunit; 3 | using FluentAssertions; 4 | using CommandLine.Tests.Fakes; 5 | using CommandLine.Text; 6 | 7 | //Issue #104 8 | //When outputting HelpText, the code will correctly list valid values for enum type options. However, if the option is a nullable enum type, then it will not list the valid values. 9 | 10 | namespace CommandLine.Tests.Unit 11 | { 12 | public class Issue104Tests 13 | { 14 | 15 | [Fact] 16 | public void Create_instance_with_enum_options_enabled_and_nullable_enum() 17 | { 18 | // Fixture setup 19 | // Exercize system 20 | var sut = new HelpText { AddDashesToOption = true, AddEnumValuesToHelpText = true, MaximumDisplayWidth = 80 } 21 | .AddPreOptionsLine("pre-options") 22 | .AddOptions(new NotParsed
PM> Install-Package CommandLineParser -Pre