This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from julien-wff/feat-base-commands
feat: version and help commands
- Loading branch information
Showing
7 changed files
with
242 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using EasyCLI.Commands.CommandFeatures; | ||
using EasyCLI.Commands.CommandFeatures.CommandArgType; | ||
|
||
namespace EasyCLI.Commands; | ||
|
||
public class HelpCommand : Command | ||
{ | ||
public override CommandBuilder.CommandBuilder Params { get; } = new CommandBuilder.CommandBuilder() | ||
.SetName("help") | ||
.SetDescription("Display help on other commands") | ||
.SetAliases(new[] { "h", "-h", "--help" }) | ||
.AddArg(new CommandArg() | ||
.SetName("command") | ||
.SetDescription("Display help of a specific command") | ||
.SetType(new CommandArgTypeString()) | ||
.SetRequired(false)); | ||
|
||
public override bool ValidateArgs(IEnumerable<string> args) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Run(IEnumerable<string> args) | ||
{ | ||
var argsList = args.ToList(); | ||
|
||
if (argsList.Count == 2) | ||
{ | ||
var commandName = argsList[1]; | ||
var command = CommandRunner | ||
.CommandRunner | ||
.GetInstance() | ||
.GetCommandFromArgs(argsList.GetRange(1, 1)); | ||
|
||
if (command is null) | ||
{ | ||
Console.WriteLine($"Command {commandName} not found. Try 'easysave help' for more information."); | ||
return; | ||
} | ||
|
||
command.ShowHelp(); | ||
} | ||
else | ||
{ | ||
PrintGeneralHelp(); | ||
} | ||
} | ||
|
||
private void PrintGeneralHelp() | ||
{ | ||
Console.WriteLine("EasySave CLI Help Page"); | ||
Console.WriteLine(); | ||
Console.WriteLine("Usage: easysave [command] [arguments]"); | ||
Console.WriteLine(); | ||
Console.WriteLine("A CLI tool to backup your files"); | ||
Console.WriteLine(); | ||
Console.WriteLine("Available commands:"); | ||
|
||
var commands = CommandRunner | ||
.CommandRunner | ||
.GetInstance() | ||
.Commands; | ||
|
||
var longestCommandName = commands | ||
.Select(command => command.Params.Name.Length) | ||
.Max(); | ||
|
||
foreach (var cmdParams in commands.Select(cmd => cmd.Params)) | ||
{ | ||
var commandName = cmdParams.Name; | ||
var commandDescription = cmdParams.Description; | ||
|
||
Console.Write($" {commandName}"); | ||
Console.Write(new string(' ', longestCommandName - commandName.Length + 2)); | ||
Console.WriteLine(commandDescription); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Reflection; | ||
|
||
namespace EasyCLI.Commands; | ||
|
||
public class VersionCommand : Command | ||
{ | ||
public override CommandBuilder.CommandBuilder Params { get; } = new CommandBuilder.CommandBuilder() | ||
.SetName("version") | ||
.SetDescription("Get the version of the application") | ||
.SetAliases(new List<string> { "ver", "v", "--version", "-v" }); | ||
|
||
public override bool ValidateArgs(IEnumerable<string> args) | ||
{ | ||
var argsList = args.ToList(); | ||
var argsCount = argsList.Count; | ||
if (argsCount != 0) | ||
{ | ||
Console.WriteLine($"Command expects 0 arguments, {argsCount} given."); | ||
ShowHelp(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public override void Run(IEnumerable<string> args) | ||
{ | ||
var version = Assembly.GetEntryAssembly()?.GetName().Version; | ||
var os = Environment.OSVersion; | ||
Console.WriteLine($"EasySave CLI version {version} on {os.VersionString}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
namespace EasyCLI; | ||
using EasyCLI.Commands; | ||
|
||
namespace EasyCLI; | ||
|
||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
throw new NotImplementedException(); | ||
CommandRunner | ||
.CommandRunner | ||
.GetInstance() | ||
.RegisterCommand(new HelpCommand()) | ||
.RegisterCommand(new VersionCommand()) | ||
.RunWithArgs(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters