Skip to content

Commit

Permalink
(chocolatey#2788) Handle limited output for removed options
Browse files Browse the repository at this point in the history
When emitting a warning for options that are no longer supported, if the
user requested limited output we should not emit a warning.
  • Loading branch information
corbob committed Mar 17, 2023
1 parent fa72702 commit e99fe33
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
103 changes: 56 additions & 47 deletions src/chocolatey/infrastructure.app/commands/ChocolateyCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
// Copyright © 2023 Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Linq;

namespace chocolatey.infrastructure.app.commands
{
/// <summary>
/// A base class for any Chocolatey commands which need to utilise shared logic.
/// </summary>
public abstract class ChocolateyCommandBase
{
/// <summary>
/// Emit a warning to the use if any of the options which are known to be deprecated are found in the <paramref name="unparsedOptions"/>.
/// </summary>
/// <param name="unparsedOptions">The list of unrecognised and unparsed options.</param>
/// <param name="removedOptions">The list of options which are known to be removed and should be warned for.</param>
protected virtual void warn_for_removed_options(IEnumerable<string> unparsedOptions, IEnumerable<string> removedOptions)
{
if (!unparsedOptions.or_empty_list_if_null().Any() || !removedOptions.or_empty_list_if_null().Any())
{
return;
}

foreach (var removed in removedOptions)
{
if (unparsedOptions.Contains(removed))
{
this.Log().Warn("The {0} option is no longer supported.", removed);
}
}
}
}
}
// Copyright © 2023 Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Linq;

namespace chocolatey.infrastructure.app.commands
{
/// <summary>
/// A base class for any Chocolatey commands which need to utilise shared logic.
/// </summary>
public abstract class ChocolateyCommandBase
{
/// <summary>
/// Emit a warning to the use if any of the options which are known to be deprecated are found in the <paramref name="unparsedOptions"/>.
/// </summary>
/// <param name="unparsedOptions">The list of unrecognised and unparsed options.</param>
/// <param name="removedOptions">The list of options which are known to be removed and should be warned for.</param>
protected virtual void warn_for_removed_options(IEnumerable<string> unparsedOptions, IEnumerable<string> removedOptions, bool RegularOutput)
{
if (!unparsedOptions.or_empty_list_if_null().Any() || !removedOptions.or_empty_list_if_null().Any())
{
return;
}

foreach (var removed in removedOptions)
{
if (unparsedOptions.Contains(removed))
{
var message = "The {0} option is no longer supported.".format_with(removed);

if (RegularOutput)
{
this.Log().Warn(message);
}
else
{
this.Log().Debug(message);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg

if (configuration.RegularOutput)
{
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions);
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions, configuration.RegularOutput);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg

if (configuration.RegularOutput)
{
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions);
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions, configuration.RegularOutput);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg

if (configuration.RegularOutput)
{
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions);
warn_for_removed_options(unparsedArguments.Where(arg => arg.StartsWith("-")), _removedOptions, configuration.RegularOutput);
}
}

Expand Down

0 comments on commit e99fe33

Please sign in to comment.