Skip to content

Commit

Permalink
Changing Parse method #157 and expose ValidationContext from command …
Browse files Browse the repository at this point in the history
…line Context
  • Loading branch information
Averus-a committed Aug 18, 2020
1 parent b7dbcf5 commit b3221be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/Orc.CommandLine/Context/ContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ namespace Orc.CommandLine
{
using System;
using System.Collections.Generic;
using Catel.Data;

public abstract class ContextBase : IContext
{
protected ContextBase()
{
RawValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
QuoteSplitCharacters = new List<char>(new [] { '\"', '\'' });

ValidationContext = new ValidationContext();
}

#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 IValidationContext ValidationContext { get; private set; }
#endregion

#region Methods
Expand All @@ -31,4 +36,4 @@ public virtual void Finish()
}
#endregion
}
}
}
18 changes: 5 additions & 13 deletions src/Orc.CommandLine/Context/Interfaces/IContext.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
// --------------------------------------------------------------------------------------------------------------------
// <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;
using Catel.Data;

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

bool IsHelp { get; set; }

Dictionary<string, string> RawValues { get; }

List<char> QuoteSplitCharacters { get; }
#endregion

#region Methods
IValidationContext ValidationContext { get; }

void Finish();
#endregion
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Orc.CommandLine

public static class ICommandLineParserExtensions
{
#region Methods
public static IEnumerable<string> GetAppHeader(this ICommandLineParser commandLineParser)
{
Argument.IsNotNull(() => commandLineParser);
Expand All @@ -33,6 +32,29 @@ public static IEnumerable<string> GetHelp(this ICommandLineParser commandLinePar
var helpWriterService = dependencyResolver.Resolve<IHelpWriterService>();
return helpWriterService.GetHelp(targetContext);
}
#endregion

public static TContext Parse<TContext>(this ICommandLineParser commandLineParser)
where TContext : IContext
{
return (TContext)commandLineParser.Parse(typeof(TContext));
}

public static TContext Parse<TContext>(this ICommandLineParser commandLineParser, List<string> commandLineArguments)
where TContext : IContext
{
return (TContext)commandLineParser.Parse(typeof(TContext), commandLineArguments);
}

public static TContext Parse<TContext>(this ICommandLineParser commandLineParser, IEnumerable<string> commandLineArguments)
where TContext : IContext
{
return (TContext)commandLineParser.Parse(typeof(TContext), commandLineArguments);
}

public static TContext Parse<TContext>(this ICommandLineParser commandLineParser, string commandLine)
where TContext : IContext
{
return (TContext)commandLineParser.Parse(typeof(TContext), commandLine);
}
}
}
}
13 changes: 13 additions & 0 deletions src/Orc.CommandLine/Parsers/Interfaces/ICommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@

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

public interface ICommandLineParser
{
[ObsoleteEx(ReplacementTypeOrMember = "IContext Parse(Type)", TreatAsErrorFromVersion = "4.0", RemoveInVersion = "5.0")]
IValidationContext Parse(IContext targetContext);

[ObsoleteEx(ReplacementTypeOrMember = "IContext Parse(Type, List<string>)", TreatAsErrorFromVersion = "4.0", RemoveInVersion = "5.0")]
IValidationContext Parse(List<string> commandLineArguments, IContext targetContext);

[ObsoleteEx(ReplacementTypeOrMember = "IContext Parse(Type, IEnumerable<string>)", TreatAsErrorFromVersion = "4.0", RemoveInVersion = "5.0")]
IValidationContext Parse(IEnumerable<string> commandLineArguments, IContext targetContext);

[ObsoleteEx(ReplacementTypeOrMember = "IContext Parse(Type, string)", TreatAsErrorFromVersion = "4.0", RemoveInVersion = "5.0")]
IValidationContext Parse(string commandLine, IContext targetContext);

IContext Parse(Type contextType);
IContext Parse(Type contextType, List<string> commandLineArguments);
IContext Parse(Type contextType, IEnumerable<string> commandLineArguments);
IContext Parse(Type contextType, string commandLine);
}
}

0 comments on commit b3221be

Please sign in to comment.