Skip to content

Commit a48be8c

Browse files
committed
[WIP] Improved CLI options
1 parent c6fbbca commit a48be8c

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/nugraph/GraphCommand.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ protected override async Task<int> ExecuteAsync(CommandContext commandContext, G
5151
if (graphUrl != null)
5252
{
5353
var url = graphUrl.ToString();
54-
// Using "console.WriteLine(url)" (lowercase c) would insert newlines at the physical console length, making the written URL neither copyable nor clickable
55-
// At that point the status has terminated so it's fine not using the IAnsiConsole methods
56-
await stdOut.WriteLineAsync(url);
57-
if (!settings.NoBrowser)
54+
if (settings.UrlAction.HasFlag(UrlAction.print))
55+
{
56+
// Using "console.WriteLine(url)" (lowercase c) would insert newlines at the physical console length, making the written URL neither copyable nor clickable
57+
// At that point the status has terminated so it's fine not using the IAnsiConsole methods
58+
await stdOut.WriteLineAsync(url);
59+
}
60+
if (settings.UrlAction.HasFlag(UrlAction.open))
5861
{
5962
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
6063
}

src/nugraph/GraphCommandSettings.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212

1313
namespace nugraph;
1414

15+
[Flags]
16+
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Better fit for command line options")]
17+
internal enum UrlAction
18+
{
19+
nothing = 0,
20+
open = 1 << 0,
21+
print = 1 << 1,
22+
}
23+
1524
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global", Justification = "Required for Spectre.Console.Cli binding")]
1625
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "Required for Spectre.Console.Cli binding")]
1726
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated by Spectre.Console.Cli through reflection")]
@@ -22,41 +31,50 @@ internal sealed class GraphCommandSettings : CommandSettings
2231

2332
[CommandArgument(0, "[SOURCE]")]
2433
[Description("The source of the graph. Can be either a directory containing a .NET project, a .NET project file (csproj/fsproj/vbproj) or the name of a NuGet package, " +
25-
"optionally with a specific version, e.g. [b]Newtonsoft.Json/13.0.3[/].")]
34+
"optionally with a specific version, e.g. [b]Newtonsoft.Json/13.0.3[/]")]
2635
public string? SourceInput { get; init; }
2736

2837
public FileOrPackage? Source { get; private set; }
2938

30-
[CommandOption("-o|--output <OUTPUT>")]
31-
[Description("The path to the dependency graph output file. If not specified, the dependency graph URL is written on the standard output and an online service is opened in the default browser.")]
32-
public FileInfo? OutputFile { get; init; }
33-
3439
[CommandOption("-f|--framework <FRAMEWORK>")]
35-
[Description("The target framework to consider when building the dependency graph.")]
40+
[Description("The target framework to consider when building the dependency graph")]
3641
[TypeConverter(typeof(NuGetFrameworkConverter))]
3742
public NuGetFramework? Framework { get; init; }
3843

3944
[CommandOption("-r|--runtime <RUNTIME_IDENTIFIER>")]
40-
[Description("The target runtime to consider when building the dependency graph.")]
45+
[Description("The target runtime to consider when building the dependency graph")]
4146
public string? RuntimeIdentifier { get; init; }
4247

48+
[CommandOption("-o|--output <OUTPUT>")]
49+
[Description("The path to the dependency graph output file\n" +
50+
"If not specified, the default web browser is used to render the dependency graph using an online service\n" +
51+
"See also the [b]--format[/] and [b]--url[/] options")]
52+
public FileInfo? OutputFile { get; init; }
53+
4354
[CommandOption("-m|--format <FORMAT>")]
44-
[Description($"The format to use when the [b]--output[/] option is not specified.\n" +
55+
[Description($"The format to use when the [b]--output[/] option is not specified\n" +
4556
$"Use [b]mmd[/] or [b]mermaid[/] for Mermaid Live Editor https://mermaid.live\n" +
4657
$"Use [b]dot[/], [b]gv[/] or [b]graphviz[/] for Edotor https://edotor.net\n" +
47-
"See https://github.com/0xced/nugraph/#output for even more supported formats.")]
58+
"See https://github.com/0xced/nugraph/#output for even more supported formats")]
4859
[DefaultValue("mermaid")]
4960
public string Format { get; init; } = "";
5061

5162
public OnlineService Service { get; private set; }
5263

64+
[CommandOption("-u|--url")]
65+
[Description("Defines what to do when the [b]--output[/] option is not specified\n" +
66+
"Use [b]open[/] for rendering the dependency graph using an online service or [b]print[/] for printing the graph URL on the standard output without opening the browser\n" +
67+
"Both options can be combined with a coma ([b]open,print[/]) to both open the URL and print it")]
68+
[DefaultValue(UrlAction.open)]
69+
public UrlAction UrlAction { get; init; }
70+
5371
[CommandOption("-d|--direction <GRAPH_DIRECTION>")]
5472
[Description($"The direction of the dependency graph. Possible values are [b]{nameof(GraphDirection.LeftToRight)}[/] and [b]{nameof(GraphDirection.TopToBottom)}[/]")]
5573
[DefaultValue(GraphDirection.LeftToRight)]
5674
public GraphDirection GraphDirection { get; init; }
5775

5876
[CommandOption("-t|--title <GRAPH_TITLE>")]
59-
[Description("The title of the dependency graph.")]
77+
[Description("The title of the dependency graph")]
6078
[DefaultValue(DefaultTitle)]
6179
public string Title { get; set; } = "";
6280

@@ -66,19 +84,14 @@ internal sealed class GraphCommandSettings : CommandSettings
6684
public bool GraphIncludeVersions { get; init; }
6785

6886
[CommandOption("-i|--ignore <PACKAGE>")]
69-
[Description("Packages to ignore in the dependency graph. Supports * wildcards. May be used multiple times.")]
87+
[Description("Packages to ignore in the dependency graph. Supports * wildcards. May be used multiple times")]
7088
public string[] GraphIgnore { get; init; } = [];
7189

7290
[CommandOption("--no-links")]
73-
[Description("Remove clickable links from the the dependency graph. Can be useful to reduce the size of the graph if you get \"Maximum text size in diagram exceeded\" in Mermaid Live Editor.")]
91+
[Description("Remove clickable links from the the dependency graph. Can be useful to reduce the size of the graph if you get \"Maximum text size in diagram exceeded\" in Mermaid Live Editor")]
7492
[DefaultValue(false)]
7593
public bool NoLinks { get; set; }
7694

77-
[CommandOption("--no-browser")]
78-
[Description("Do not open the default browser, only print the graph URL on the console when the [b]--output[/] option is not specified.")]
79-
[DefaultValue(false)]
80-
public bool NoBrowser { get; set; }
81-
8295
[CommandOption("-l|--log <LEVEL>")]
8396
[Description($"The NuGet operations log level. Possible values are [b]{nameof(LogLevel.Debug)}[/], [b]{nameof(LogLevel.Verbose)}[/], [b]{nameof(LogLevel.Information)}[/], [b]{nameof(LogLevel.Minimal)}[/], [b]{nameof(LogLevel.Warning)}[/] and [b]{nameof(LogLevel.Error)}[/]")]
8497
#if DEBUG
@@ -89,20 +102,20 @@ internal sealed class GraphCommandSettings : CommandSettings
89102
public LogLevel LogLevel { get; init; }
90103

91104
[CommandOption("--nuget-root <PATH>", IsHidden = true)]
92-
[Description("The NuGet root directory. Can be used to completely isolate nugraph from default NuGet operations.")]
105+
[Description("The NuGet root directory, can be used to completely isolate nugraph from default NuGet operations")]
93106
public string? NuGetRoot { get; init; }
94107

95108
[CommandOption("--sdk <PATH>", IsHidden = true)]
96109
[Description("Path to the .NET SDK directory. E.g. [b]/usr/local/share/dotnet/sdk/8.0.410[/]")]
97110
public DirectoryInfo? Sdk { get; init; }
98111

99112
[CommandOption("--include-ignored-packages", IsHidden = true)]
100-
[Description("Include ignored packages in the dependency graph. Used for debugging.")]
113+
[Description("Include ignored packages in the dependency graph, used for debugging")]
101114
[DefaultValue(false)]
102115
public bool GraphWriteIgnoredPackages { get; init; }
103116

104117
[CommandOption("--diagnose", IsHidden = true)]
105-
[Description("Prints diagnostics information.")]
118+
[Description("Prints diagnostics information")]
106119
[DefaultValue(false)]
107120
public bool Diagnose { get; init; }
108121

0 commit comments

Comments
 (0)