Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
dedouwe26 committed Dec 30, 2024
1 parent f5bb5bf commit 842eb95
Show file tree
Hide file tree
Showing 12 changed files with 787 additions and 696 deletions.
22 changes: 0 additions & 22 deletions .vscode/launch.json

This file was deleted.

15 changes: 0 additions & 15 deletions .vscode/tasks.json

This file was deleted.

130 changes: 30 additions & 100 deletions Terminal/Arguments/Argument.cs
Original file line number Diff line number Diff line change
@@ -1,137 +1,67 @@
namespace OxDED.Terminal.Arguments;

/// <summary>
/// Represents an optional argument (-f, --foo).
/// Represents a required argument in a specific order.
/// </summary>
public class Argument : ICloneable, IEquatable<Argument> {
public class Argument : ICloneable {
/// <summary>
/// The keys of this argument (f, foo).
/// The name (key) of this argument.
/// </summary>
public string[] keys;
public string name;
/// <summary>
/// The parameters of this argument.
/// The description of this argument.
/// </summary>
public ArgumentParameter[] parameters;
public string? description;
internal string? value;
/// <summary>
/// The description of this argument.
/// If this argument has a value (should be yes).
/// </summary>
public string? description = null;
public bool HasValue { get => value != null; }
/// <summary>
/// Creates an argument.
/// The value of this argument (error if it isn't parsed).
/// </summary>
/// <param name="key">The key of this argument.</param>
/// <param name="description">The description of this argument (optional).</param>
/// <param name="parameters">The parameters of this argument (default: empty).</param>
public Argument(string key, string? description = null, IEnumerable<ArgumentParameter>? parameters = null) {
keys = [key];
this.description = description;
this.parameters = parameters == null ? [] : [.. parameters];
}
/// <exception cref="InvalidOperationException"/>
public string Value { get {
if (HasValue) {
return value!;
} else {
throw new InvalidOperationException("This argument has not yet been parsed");
}
} }

/// <summary>
/// Creates an argument.
/// Creates a argument.
/// </summary>
/// <param name="keys">The keys of this argument.</param>
/// <param name="name">The name of this argument.</param>
/// <param name="description">The description of this argument (optional).</param>
/// <param name="parameters">The parameters of this argument (default: empty).</param>
public Argument(IEnumerable<string> keys, string? description = null, IEnumerable<ArgumentParameter>? parameters = null) {
this.keys = [.. keys];
public Argument(string name, string? description = null) {
this.name = name;
this.description = description;
this.parameters = parameters == null ? [] : [.. parameters];
}
/// <summary>
/// Sets the key of this argument.
/// Sets the name of this argument.
/// </summary>
/// <param name="key">The new key.</param>
/// <param name="name">The new name of this argument.</param>
/// <returns>This argument.</returns>
public Argument Key(string key) {
keys = [key];
return this;
}
/// <summary>
/// Sets the keys of this argument.
/// </summary>
/// <param name="keys">The new keys.</param>
/// <returns>This argument.</returns>
public Argument Keys(IEnumerable<string> keys) {
this.keys = [.. keys];
public Argument Name(string name) {
this.name = name;
return this;
}
/// <summary>
/// Sets the description of this argument.
/// </summary>
/// <param name="description">The new description.</param>
/// <param name="description">The new description of this argument.</param>
/// <returns>This argument.</returns>
public Argument Description(string? description) {
this.description = description;
return this;
}
/// <summary>
/// Sets the parameters of this argument.
/// </summary>
/// <param name="parameters">The new parameters.</param>
/// <returns>This argument.</returns>
public Argument Parameters(IEnumerable<ArgumentParameter> parameters) {
this.parameters = [.. parameters];
return this;
}
/// <summary>
/// Adds a parameter to this argument.
/// </summary>
/// <param name="parameter">The parameter to add.</param>
/// <returns>This argument.</returns>
public Argument AddParameter(ArgumentParameter parameter) {
parameters = [.. parameters, parameter];
return this;
}
/// <summary>
/// If this argument's parameters have values (should be yes).
/// </summary>
public bool HasValue { get => parameters.All((ArgumentParameter parameter) => parameter.HasValue); }

///
public static bool operator ==(Argument? left, Argument? right) {
if (left is null && right is null) {
return true;
} else if (left is null) {
return false;
}
return left.Equals(right);
}
///
public static bool operator !=(Argument? left, Argument? right) {
return !(left == right);
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public bool Equals(Argument? other) {
if (other is null) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
if (GetType() != other.GetType()) {
return false;
}
return keys == other.keys;
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public override bool Equals(object? obj) {
return Equals(obj as Color);
}
/// <inheritdoc/>
public override int GetHashCode() {
return keys.GetHashCode();
}
/// <inheritdoc/>
/// <remarks>
/// Calls <see cref="CloneArgument"/>.
/// </remarks>

public object Clone() {
return CloneArgument();
}
Expand All @@ -142,6 +72,6 @@ public object Clone() {
/// <returns>The new copy of this color.</returns>
/// <exception cref="InvalidOperationException"/>
public Argument CloneArgument() {
return new Argument(keys, description, parameters);
return new Argument(name, description);
}
}
174 changes: 174 additions & 0 deletions Terminal/Arguments/ArgumentFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
namespace OxDED.Terminal.Arguments;

/// <summary>
/// Represents a format for arguments and options.
/// </summary>
public partial class ArgumentFormatter {
public string? name;
public string? description;
public string? version;

/// <summary>
/// The options of this format.
/// </summary>
public List<OptionFormat> Options { get; private set; }
/// <summary>
/// The arguments of this format.
/// </summary>
public List<ArgumentFormat> Arguments { get; private set; }

/// <summary>
/// Creates a new argument format.
/// </summary>
public ArgumentFormatter(List<ArgumentFormat>? arguments = null, List<OptionFormat>? options = null) {
Options = options ?? [];
Arguments = arguments ?? [];
}

public OptionFormat Option() {
return new(this);
}
public ArgumentFormat Argument() {
return new(this);
}

public ArgumentFormatter Name(string name) {
this.name = name;
return this;
}
public ArgumentFormatter Description(string? description) {
this.description = description;
return this;
}
public ArgumentFormatter Version(string? version) {
this.version = version;
return this;
}

public ArgumentFormatter AddHelpOption(bool quit = true, IEnumerable<string>? keys = null) {
Option()
.Keys(keys ?? ["-h", "--help"])
.Description("Shows this help message.")
.Finish();
return this;
}
public ArgumentFormatter AddVersionOption(bool quit = true, IEnumerable<string>? keys = null) {
Option()
.Keys(keys ?? ["-v", "--version"])
.Description("Shows the version of this application.")
.Finish();
return this;
}

// public ArgumentFormatter
}

public partial class ArgumentFormatter {
public class ArgumentFormat {
public string description;
public string name;

public ArgumentFormatter ArgumentFormatter { get; private set; }

public ArgumentFormat(ArgumentFormatter argumentFormatter) {
ArgumentFormatter = argumentFormatter;
}

public ArgumentFormat Name(string name) {
this.name = name;
return this;
}
public ArgumentFormat Description(string description) {
this.description = description;
return this;
}

public ArgumentFormatter Finish() {
ArgumentFormatter.Arguments = [.. ArgumentFormatter.Arguments, this];
return ArgumentFormatter;
}
}
}

public partial class ArgumentFormatter {
public class OptionFormat {
public string[] keys;
public string description;
public ParameterFormat[] parameters;

public ArgumentFormatter ArgumentFormatter { get; private set; }

public OptionFormat(ArgumentFormatter argumentFormatter) {
ArgumentFormatter = argumentFormatter;
}

public OptionFormat Key(string key) {
keys = [.. keys, key];
return this;
}
public OptionFormat Keys(IEnumerable<string> keys) {
this.keys = [.. this.keys, .. keys];
return this;
}
public OptionFormat Description(string description) {
this.description = description;
return this;
}
public ParameterFormat Parameter() {
return new(this);
}

public ArgumentFormatter Finish() {
ArgumentFormatter.Options = [.. ArgumentFormatter.Options, this];
return ArgumentFormatter;
}

public class ParameterFormat {
public string name { get; set; }
public string description { get; set; }
public bool required { get; set; }

public OptionFormat OptionFormat { get; private set; }

public ParameterFormat(OptionFormat optionBuilder) {
OptionFormat = optionBuilder;
}

public ParameterFormat Name(string name) {
this.name = name;
return this;
}
public ParameterFormat Description(string description) {
this.description = description;
return this;
}
public ParameterFormat Required(bool required) {
this.required = required;
return this;
}

public OptionFormat Finish() {
OptionFormat.parameters = [.. OptionFormat.parameters, this];
return OptionFormat;
}
}
}
}

public class HelpOptionFormat : ArgumentFormatter.OptionFormat {
public HelpOptionFormat(ArgumentFormatter argumentFormatter) : base(argumentFormatter) { }
public bool quit;
public HelpOptionFormat Quit(bool quit) {
this.quit = quit;
return this;
}
}

public class VersionOptionFormat : ArgumentFormatter.OptionFormat {
public VersionOptionFormat(ArgumentFormatter argumentFormatter) : base(argumentFormatter) { }
public bool quit;
public VersionOptionFormat Quit(bool quit) {
this.quit = quit;
return this;
}
}
Loading

0 comments on commit 842eb95

Please sign in to comment.