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

Add option '--function' #87

Merged
merged 9 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add option `--function` which replaces `--modify` ([#87](https://github.com/josefpihrt/orang/pull/87)).

### Changed

- Make option `--modify` obsolete ([#87](https://github.com/josefpihrt/orang/pull/87)).

### Fixed

- [CLI] Fix reading of redirected input ([PR](https://github.com/dotnet/roslynator/pull/91))
Expand Down
7 changes: 5 additions & 2 deletions src/CommandLine/Aggregation/AggregateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ public void WriteAggregatedValues(CancellationToken cancellationToken)
{
int groupCount = valuesMap[en.Current].Count;

_logger.Write(" ", Colors.Message_OK, Verbosity.Minimal);
_logger.Write(groupCount.ToString("n0"), Colors.Message_OK, Verbosity.Minimal);
if (groupCount > 1)
{
_logger.Write(" ", Colors.Message_OK, Verbosity.Minimal);
_logger.Write(groupCount.ToString("n0"), Colors.Message_OK, Verbosity.Minimal);
}
}

_logger.WriteLine(Verbosity.Minimal);
Expand Down
9 changes: 9 additions & 0 deletions src/CommandLine/CommandLineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ namespace Orang;

internal static class CommandLineExtensions
{
public static void WriteDeprecatedWarning(
this Logger logger,
string message,
ConsoleColors colors = default,
Verbosity verbosity = Verbosity.Minimal)
{
logger.ConsoleOut.ErrorWriter.WriteLine(message, (colors.IsDefault) ? Colors.Message_Warning : colors, verbosity);
}

public static void WriteWarning(
this Logger logger,
string message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ namespace Orang.CommandLine;

internal abstract class CommonCopyCommandLineOptions : CommonFindCommandLineOptions
{
public override ContentDisplayStyle DefaultContentDisplayStyle => ContentDisplayStyle.Omit;
public override ContentDisplayStyle GetDefaultContentDisplayStyle()
{
return ContentDisplayStyle.Omit;
}

[Option(
longName: OptionNames.Compare,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Orang.CommandLine;
[OptionValueProvider(nameof(Highlight), OptionValueProviderNames.FindHighlightOptions)]
internal abstract class CommonFindCommandLineOptions : FileSystemCommandLineOptions
{
public abstract ContentDisplayStyle DefaultContentDisplayStyle { get; }

[Option(
shortName: OptionShortNames.Content,
longName: OptionNames.Content,
Expand Down Expand Up @@ -88,6 +86,9 @@ public bool TryParse(CommonFindCommandOptions options, ParseContext context)
out ContentDisplayStyle contentDisplayStyle2,
provider: OptionValueProviders.ContentDisplayStyleProvider))
{
if (!VerifyContentDisplayStyle(contentDisplayStyle2, context))
return false;

contentDisplayStyle = contentDisplayStyle2;
}
else
Expand Down Expand Up @@ -178,7 +179,7 @@ public bool TryParse(CommonFindCommandOptions options, ParseContext context)

options.Format = new OutputDisplayFormat(
contentDisplayStyle: contentDisplayStyle
?? ((ReferenceEquals(contentFilter, Matcher.EntireInput)) ? ContentDisplayStyle.AllLines : DefaultContentDisplayStyle),
?? ((ReferenceEquals(contentFilter, Matcher.EntireInput)) ? ContentDisplayStyle.AllLines : GetDefaultContentDisplayStyle()),
pathDisplayStyle: pathDisplayStyle ?? PathDisplayStyle.Full,
lineOptions: lineDisplayOptions,
lineContext: lineContext,
Expand All @@ -196,4 +197,11 @@ public bool TryParse(CommonFindCommandOptions options, ParseContext context)

return true;
}

public abstract ContentDisplayStyle GetDefaultContentDisplayStyle();

public virtual bool VerifyContentDisplayStyle(ContentDisplayStyle contentDisplayStyle, ParseContext context)
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public bool TryParse(FileSystemCommandOptions options, ParseContext context)

if (IncludeDirectory.Any())
{
context.WriteWarning($"Option '{OptionNames.GetHelpText(OptionNames.IncludeDirectory)}' has been deprecated "
context.WriteDeprecatedWarning($"Option '{OptionNames.GetHelpText(OptionNames.IncludeDirectory)}' has been deprecated "
+ "and will be removed in future versions. "
+ $"Use options '{OptionNames.GetHelpText(OptionNames.Include)}/{OptionNames.GetHelpText(OptionNames.Exclude)}' instead.");
}
Expand Down
102 changes: 85 additions & 17 deletions src/CommandLine/CommandLineOptions/FindCommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using CommandLine;
using Orang.CommandLine.Annotations;
using Orang.FileSystem;
Expand All @@ -14,8 +15,6 @@ namespace Orang.CommandLine;
[CommandGroup("Main", 0)]
internal sealed class FindCommandLineOptions : CommonFindCommandLineOptions
{
public override ContentDisplayStyle DefaultContentDisplayStyle => ContentDisplayStyle.Line;

[Option(
longName: OptionNames.Ask,
HelpText = "Ask for permission after each file or value.",
Expand All @@ -28,10 +27,18 @@ internal sealed class FindCommandLineOptions : CommonFindCommandLineOptions
MetaValue = MetaValues.PipeMode)]
public string Pipe { get; set; } = null!;

[Option(
longName: OptionNames.Function,
HelpText = "Space separated list of functions to modify a list of matches.",
MetaValue = MetaValues.Function)]
[AdditionalDescription(" All matches from all files are evaluated at once.")]
public IEnumerable<string> Function { get; set; } = null!;

[Option(
longName: OptionNames.Modify,
HelpText = "Functions to modify results.",
HelpText = "[deprecated] Modify results according to specified values.",
MetaValue = MetaValues.ModifyOptions)]
[HideFromHelp]
public IEnumerable<string> Modify { get; set; } = null!;

[Option(
Expand Down Expand Up @@ -109,28 +116,69 @@ public bool TryParse(FindCommandOptions options, ParseContext context)
}
}

if (!context.TryParseModifyOptions(
Modify,
OptionNames.Modify,
out ModifyOptions? modifyOptions,
out bool aggregateOnly))
options.ModifyOptions = ModifyOptions.Default;

if (Modify.Any())
{
return false;
context.WriteDeprecatedWarning($"Option '{OptionNames.GetHelpText(OptionNames.Modify)}' has been deprecated "
+ "and will be removed in future versions. "
+ $"Use option '{OptionNames.GetHelpText(OptionNames.Function)}' instead.");

if (!context.TryParseModifyOptions(
Modify,
OptionNames.Modify,
out ModifyOptions? modifyOptions,
out bool aggregateOnly))
{
return false;
}

options.ModifyOptions = modifyOptions;
options.AggregateOnly = aggregateOnly;
}

if (Function.Any())
{
if (Modify.Any())
{
context.WriteError($"Options '{OptionNames.GetHelpText(OptionNames.Modify)}' and "
+ $"'{OptionNames.GetHelpText(OptionNames.Function)}' cannot be used both at the same time.");

return false;
}

if (options.ContentFilter is null)
{
context.WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Content)}' is required when "
+ $"option '{OptionNames.GetHelpText(OptionNames.Function)}' is used.");

return false;
}

if (!context.TryParseFunctions(
Function,
OptionNames.Function,
out ModifyFunctions? functions,
out ValueSortProperty sortProperty))
{
return false;
}

options.ModifyOptions = new ModifyOptions(
functions.Value,
aggregate: true,
ignoreCase: (options.ContentFilter.Regex.Options & RegexOptions.IgnoreCase) != 0,
cultureInvariant: (options.ContentFilter.Regex.Options & RegexOptions.CultureInvariant) != 0,
sortProperty: sortProperty);

options.AggregateOnly = true;
}

OutputDisplayFormat format = options.Format;
ContentDisplayStyle contentDisplayStyle = format.ContentDisplayStyle;
PathDisplayStyle pathDisplayStyle = format.PathDisplayStyle;

if (modifyOptions.HasAnyFunction
&& contentDisplayStyle == ContentDisplayStyle.ValueDetail)
{
contentDisplayStyle = ContentDisplayStyle.Value;
}

options.Input = input;
options.ModifyOptions = modifyOptions;
options.AggregateOnly = aggregateOnly;
options.Split = Split;

options.Format = new OutputDisplayFormat(
Expand All @@ -144,4 +192,24 @@ public bool TryParse(FindCommandOptions options, ParseContext context)

return true;
}

public override ContentDisplayStyle GetDefaultContentDisplayStyle()
{
return (Function.Any()) ? ContentDisplayStyle.Value : ContentDisplayStyle.Line;
}

public override bool VerifyContentDisplayStyle(ContentDisplayStyle contentDisplayStyle, ParseContext context)
{
if (Function.Any()
&& contentDisplayStyle != ContentDisplayStyle.Value)
{
context.WriteError($"When option '{OptionNames.GetHelpText(OptionNames.Function)}' is used "
+ $"option '{OptionNames.GetHelpText(OptionNames.ContentMode)}' must be set to "
+ $"'{OptionValues.ContentDisplayStyle_Value.HelpValue}' (or unspecified).");

return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ internal sealed class HelpCommandLineOptions : AbstractCommandLineOptions
public bool TryParse(HelpCommandOptions options, ParseContext context)
{
if (Filter.Any())
context.WriteWarning($"Option '{OptionNames.GetHelpText(OptionNames.Filter)}' has been deprecated.");
context.WriteDeprecatedWarning($"Option '{OptionNames.GetHelpText(OptionNames.Filter)}' has been deprecated.");

if (Online)
context.WriteWarning($"Option '{OptionNames.GetHelpText(OptionNames.Online)}' has been deprecated.");
context.WriteDeprecatedWarning($"Option '{OptionNames.GetHelpText(OptionNames.Online)}' has been deprecated.");

options.Command = Command.ToArray();
options.Manual = Manual;
Expand Down
84 changes: 70 additions & 14 deletions src/CommandLine/CommandLineOptions/RegexCommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using CommandLine;
using Orang.CommandLine.Annotations;
using Orang.Text.RegularExpressions;
Expand All @@ -18,17 +19,33 @@ internal abstract class RegexCommandLineOptions : CommonRegexCommandLineOptions
MetaName = ArgumentMetaNames.Path)]
public string Path { get; set; } = null!;

[Option(
shortName: OptionShortNames.Content,
longName: OptionNames.Content,
Required = true,
HelpText = "Regular expression for the input string.",
MetaValue = MetaValues.Regex)]
public IEnumerable<string> Content { get; set; } = null!;

[Option(
shortName: OptionShortNames.Input,
longName: OptionNames.Input,
HelpText = "The input string to be searched.",
MetaValue = MetaValues.Input)]
public IEnumerable<string> Input { get; set; } = null!;

[Option(
longName: OptionNames.Function,
HelpText = "Space separated list of functions to modify a list of matches.",
MetaValue = MetaValues.Function)]
[AdditionalDescription(" All matches from all files are evaluated at once.")]
public IEnumerable<string> Function { get; set; } = null!;

[Option(
longName: OptionNames.Modify,
HelpText = "Functions to modify results.",
HelpText = "[deprecated] Modify results according to specified values.",
MetaValue = MetaValues.ModifyOptions)]
[HideFromHelp]
public IEnumerable<string> Modify { get; set; } = null!;

public bool TryParse(RegexCommandOptions options, ParseContext context)
Expand Down Expand Up @@ -115,23 +132,64 @@ public bool TryParse(RegexCommandOptions options, ParseContext context)
if (ContentSeparator is not null)
separator = ContentSeparator;
#endif
if (!context.TryParseModifyOptions(
Modify,
OptionNames.Modify,
out ModifyOptions? modifyOptions,
out bool aggregateOnly))

options.ModifyOptions = ModifyOptions.Default;

if (Modify.Any())
{
return false;
context.WriteDeprecatedWarning($"Option '{OptionNames.GetHelpText(OptionNames.Modify)}' has been deprecated "
+ "and will be removed in future versions. "
+ $"Use option '{OptionNames.GetHelpText(OptionNames.Function)}' instead.");

if (!context.TryParseModifyOptions(
Modify,
OptionNames.Modify,
out ModifyOptions? modifyOptions,
out bool aggregateOnly))
{
return false;
}

options.ModifyOptions = modifyOptions;

if (aggregateOnly)
context.Logger.ConsoleOut.Verbosity = Orang.Verbosity.Minimal;

if (modifyOptions.HasAnyFunction
&& contentDisplayStyle == ContentDisplayStyle.ValueDetail)
{
contentDisplayStyle = ContentDisplayStyle.Value;
}
}

if (modifyOptions.HasAnyFunction
&& contentDisplayStyle == ContentDisplayStyle.ValueDetail)
if (Function.Any())
{
contentDisplayStyle = ContentDisplayStyle.Value;
}
if (Modify.Any())
{
context.WriteError($"Options '{OptionNames.GetHelpText(OptionNames.Modify)}' and "
+ $"'{OptionNames.GetHelpText(OptionNames.Function)}' cannot be used both at the same time.");

return false;
}

if (!context.TryParseFunctions(
Function,
OptionNames.Function,
out ModifyFunctions? functions,
out ValueSortProperty sortProperty))
{
return false;
}

options.ModifyOptions = new ModifyOptions(
functions.Value,
aggregate: true,
ignoreCase: (options.Matcher.Regex.Options & RegexOptions.IgnoreCase) != 0,
cultureInvariant: (options.Matcher.Regex.Options & RegexOptions.CultureInvariant) != 0,
sortProperty: sortProperty);

if (aggregateOnly)
context.Logger.ConsoleOut.Verbosity = Orang.Verbosity.Minimal;
}

options.Format = new OutputDisplayFormat(
contentDisplayStyle: contentDisplayStyle ?? ContentDisplayStyle.Value,
Expand All @@ -144,8 +202,6 @@ public bool TryParse(RegexCommandOptions options, ParseContext context)
#else
separator: Environment.NewLine);
#endif

options.ModifyOptions = modifyOptions;
options.Input = input;

return true;
Expand Down
Loading