Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/orcomp 644 #278

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/Orc.CommandLine.Tests/Orc.CommandLine.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,44 @@ public CommandLineException(string message, System.Exception innerException) { }
public class CommandLineParser : Orc.CommandLine.ICommandLineParser
{
public CommandLineParser(Orc.CommandLine.IOptionDefinitionService optionDefinitionService, Catel.Services.ILanguageService languageService, Orc.CommandLine.ICommandLineService commandLineService) { }
protected virtual System.Text.RegularExpressions.Regex CreateRegex(Orc.CommandLine.IContext targetContext) { }
protected virtual System.Text.RegularExpressions.Regex CreateRegex(Orc.CommandLine.ICommandLineParsingContext targetContext) { }
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
public Catel.Data.IValidationContext Parse(Orc.CommandLine.IContext targetContext) { }
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
public Catel.Data.IValidationContext Parse(System.Collections.Generic.IEnumerable<string> commandLineArguments, Orc.CommandLine.IContext targetContext) { }
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
public Catel.Data.IValidationContext Parse(System.Collections.Generic.List<string> commandLineArguments, Orc.CommandLine.IContext targetContext) { }
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
public Catel.Data.IValidationContext Parse(string commandLine, Orc.CommandLine.IContext targetContext) { }
public TResult Parse<TResult>(Orc.CommandLine.ICommandLineParsingContext commandLineParsingContext)
where TResult : Orc.CommandLine.IResult { }
protected virtual void ValidateMandatorySwitches(Catel.Data.IValidationContext validationContext, System.Collections.Generic.IEnumerable<Orc.CommandLine.OptionDefinition> optionDefinitions, System.Collections.Generic.HashSet<string> handledOptions) { }
}
public class CommandLineParsingContextBase : Orc.CommandLine.ICommandLineParsingContext
{
public CommandLineParsingContextBase() { }
public CommandLineParsingContextBase(string commandLine) { }
public string CommandLine { get; set; }
public System.Collections.Generic.List<char> QuoteSplitCharacters { get; }
}
public class CommandLineService : Orc.CommandLine.ICommandLineService
{
public CommandLineService() { }
public virtual string GetCommandLine() { }
}
public abstract class ContextBase : Orc.CommandLine.IContext
public abstract class ContextBase : Orc.CommandLine.ICommandLineParsingContext, Orc.CommandLine.IContext, Orc.CommandLine.IResult
{
protected ContextBase() { }
public string CommandLine { get; set; }
public bool IsHelp { get; set; }
public string OriginalCommandLine { get; set; }
public System.Collections.Generic.List<char> QuoteSplitCharacters { get; }
public System.Collections.Generic.Dictionary<string, string> RawValues { get; }
public Catel.Data.ValidationContext ValidationContext { get; set; }
public virtual void Finish() { }
}
public class HelpWriterService : Orc.CommandLine.IHelpWriterService
Expand All @@ -44,25 +63,39 @@ public System.Collections.Generic.IEnumerable<string> GetHelp(Orc.CommandLine.IC
}
public interface ICommandLineParser
{
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
Catel.Data.IValidationContext Parse(Orc.CommandLine.IContext targetContext);
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
Catel.Data.IValidationContext Parse(System.Collections.Generic.IEnumerable<string> commandLineArguments, Orc.CommandLine.IContext targetContext);
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
Catel.Data.IValidationContext Parse(System.Collections.Generic.List<string> commandLineArguments, Orc.CommandLine.IContext targetContext);
[System.Obsolete("Use `Parse<T>(ICommandLineParsingContext commandLineParsingContext)` instead. Wil" +
"l be treated as an error from version 4.2.0. Will be removed in version 5.0.0.", false)]
Catel.Data.IValidationContext Parse(string commandLine, Orc.CommandLine.IContext targetContext);
T Parse<T>(Orc.CommandLine.ICommandLineParsingContext commandLineParsingContext)
where T : Orc.CommandLine.IResult;
}
public static class ICommandLineParserExtensions
{
public static System.Collections.Generic.IEnumerable<string> GetAppHeader(this Orc.CommandLine.ICommandLineParser commandLineParser) { }
public static System.Collections.Generic.IEnumerable<string> GetHelp(this Orc.CommandLine.ICommandLineParser commandLineParser, Orc.CommandLine.IContext targetContext) { }
}
public interface ICommandLineParsingContext
{
string CommandLine { get; set; }
System.Collections.Generic.List<char> QuoteSplitCharacters { get; }
}
public interface ICommandLineService
{
string GetCommandLine();
}
public interface IContext
public interface IContext : Orc.CommandLine.ICommandLineParsingContext
{
bool IsHelp { get; set; }
string OriginalCommandLine { get; set; }
System.Collections.Generic.List<char> QuoteSplitCharacters { get; }
System.Collections.Generic.Dictionary<string, string> RawValues { get; }
void Finish();
}
Expand All @@ -75,6 +108,14 @@ public interface IOptionDefinitionService
{
System.Collections.Generic.IEnumerable<Orc.CommandLine.OptionDefinition> GetOptionDefinitions(Orc.CommandLine.IContext targetContext);
}
public interface IResult : Orc.CommandLine.ICommandLineParsingContext, Orc.CommandLine.IContext
{
Catel.Data.ValidationContext ValidationContext { get; set; }
}
public interface IValidatedResult : Orc.CommandLine.ICommandLineParsingContext, Orc.CommandLine.IContext
{
Catel.Data.IValidationContext ValidationContext { get; set; }
}
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.All)]
public class OptionAttribute : System.Attribute
{
Expand Down Expand Up @@ -113,6 +154,17 @@ public class OptionDefinitionService : Orc.CommandLine.IOptionDefinitionService
public OptionDefinitionService() { }
public System.Collections.Generic.IEnumerable<Orc.CommandLine.OptionDefinition> GetOptionDefinitions(Orc.CommandLine.IContext targetContext) { }
}
public abstract class ResultBase : Orc.CommandLine.ICommandLineParsingContext, Orc.CommandLine.IContext, Orc.CommandLine.IResult
{
protected ResultBase() { }
public string CommandLine { get; set; }
public bool IsHelp { get; set; }
public string OriginalCommandLine { get; set; }
public System.Collections.Generic.List<char> QuoteSplitCharacters { get; set; }
public System.Collections.Generic.Dictionary<string, string> RawValues { get; set; }
public Catel.Data.ValidationContext ValidationContext { get; set; }
public void Finish() { }
}
public static class StringExtensions
{
public static bool IsHelp(this string singleArgument, char[] quoteSplitCharacters) { }
Expand Down
20 changes: 20 additions & 0 deletions src/Orc.CommandLine/Context/CommandLineParsingContextBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Orc.CommandLine
{
using System.Collections.Generic;
public class CommandLineParsingContextBase : ICommandLineParsingContext
{
public CommandLineParsingContextBase(string commandLine)
{
QuoteSplitCharacters = new List<char>(new[] { '\"', '\'' });
CommandLine = commandLine;
}

public CommandLineParsingContextBase()
{
}

public List<char> QuoteSplitCharacters { get; }

public string CommandLine { get; set; }
}
}
12 changes: 10 additions & 2 deletions src/Orc.CommandLine/Context/ContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace Orc.CommandLine
{
using System;
using System.Collections.Generic;
using Catel.Data;

public abstract class ContextBase : IContext
public abstract class ContextBase : IContext, IResult
{
protected ContextBase()
{
Expand All @@ -20,9 +21,16 @@ protected ContextBase()

#region Properties
public string OriginalCommandLine { get; set; }

public bool IsHelp { get; set; }

public Dictionary<string, string> RawValues { get; private set; }

public List<char> QuoteSplitCharacters { get; }

public string CommandLine { get; set; }

public ValidationContext ValidationContext { get; set; }
#endregion

#region Methods
Expand All @@ -31,4 +39,4 @@ public virtual void Finish()
}
#endregion
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Orc.CommandLine
{
using System.Collections.Generic;

public interface ICommandLineParsingContext
{
string CommandLine { get; set; }
List<char> QuoteSplitCharacters { get; }
}
}
15 changes: 3 additions & 12 deletions src/Orc.CommandLine/Context/Interfaces/IContext.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IContext.cs" company="WildGums">
// Copyright (c) 2008 - 2015 WildGums. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Orc.CommandLine
namespace Orc.CommandLine
{
using System.Collections.Generic;

public interface IContext
public interface IContext : ICommandLineParsingContext
{
#region Properties
string OriginalCommandLine { get; set; }

bool IsHelp { get; set; }

Dictionary<string, string> RawValues { get; }

List<char> QuoteSplitCharacters { get; }
#endregion

#region Methods
void Finish();
#endregion
}
}
}
11 changes: 11 additions & 0 deletions src/Orc.CommandLine/Context/Interfaces/IResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Orc.CommandLine
{
using Catel.Data;

public interface IResult : IContext
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one of the points of the issue is to split IContext into IResult and ICommandLineParsingContext
it means IResult should not be inherited from IContext

I would extract two different interfaces from IContext: ICommandLineParsingContext and something like ICommandLineParsingResult and do something like that:

[ObsoleteEx]
public interface IContext : ICommandLineParsingContext, ICommandLineParsingResult
{
    // put obsolete props and methods here
}

public interface IValidatedResult : ICommandLineParsingResult
{
   IValidationContext ValidationContext { get; }
}

{
#region Properties
ValidationContext ValidationContext { get; set; }
#endregion
}
}
9 changes: 9 additions & 0 deletions src/Orc.CommandLine/Context/Interfaces/IValidatedResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Orc.CommandLine
{
using Catel.Data;

public interface IValidatedResult : IContext
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read the comments for the IResult

{
public IValidationContext ValidationContext { get; set; }
}
}
24 changes: 24 additions & 0 deletions src/Orc.CommandLine/Context/ResultBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Orc.CommandLine
{
using System.Collections.Generic;
using Catel.Data;

public abstract class ResultBase : IResult
{
public string OriginalCommandLine { get; set; }

public bool IsHelp { get; set; }

public Dictionary<string, string> RawValues { get; set; }

public ValidationContext ValidationContext { get; set; }

public string CommandLine { get; set; }

public List<char> QuoteSplitCharacters { get; set; }

public void Finish()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why we need it

{
}
}
}
37 changes: 37 additions & 0 deletions src/Orc.CommandLine/Helpers/PropertyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Orc.CommandLine
{
using System;
using System.Reflection;

internal static class PropertyHelper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename the class to ObjectExtensions

{

internal static void SetPrivatePropertyValue(this object obj, string propName, object value)
{
var objType = obj.GetType();
var property = objType.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (property is null)
{
throw new InvalidOperationException($"Property {propName} was not found in Type {objType}");
}
var setter = property.GetSetMethod(nonPublic: true);
if (setter is null)
{
var backingField = property.DeclaringType.GetField($"<{property.Name}>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance);
if (backingField is null)
{
throw new InvalidOperationException(
$"Could not find a way to set {property.DeclaringType.FullName}.{property.Name}. Try adding a private setter.");
}
backingField.SetValue(obj, value);
}
else
{
setter.Invoke(obj, new object[]
{
value
});
}
}
}
}
Loading