Skip to content

Commit

Permalink
Set the source argument as required.
Browse files Browse the repository at this point in the history
Update CommandLineParser nuget package
  • Loading branch information
CrustyApplesniffer committed Jul 4, 2019
1 parent 7469dd7 commit 24c6504
Show file tree
Hide file tree
Showing 7 changed files with 703 additions and 7 deletions.
4 changes: 2 additions & 2 deletions AudioBookSplitterCmd/AudioBookSplitterCmd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine, Version=2.4.3.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.2.4.3\lib\netstandard2.0\CommandLine.dll</HintPath>
<Reference Include="CommandLine, Version=2.5.0.0, Culture=neutral, PublicKeyToken=5a870481e358d379, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.2.5.0\lib\net461\CommandLine.dll</HintPath>
</Reference>
<Reference Include="Humanizer, Version=2.5.0.0, Culture=neutral, PublicKeyToken=979442b78dfc278e, processorArchitecture=MSIL">
<HintPath>..\packages\Humanizer.Core.2.5.16\lib\netstandard2.0\Humanizer.dll</HintPath>
Expand Down
4 changes: 2 additions & 2 deletions AudioBookSplitterCmd/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace AudioBookSplitterCmd
{
public class Options
{
[Option('s', HelpText="MP3 file to split")]
[Option('s',"Source",Required = true, HelpText="MP3 file to split")]
public string SourceFile { get; set; }

[Option('o', HelpText = "The folder where the files will be stored.")]
[Option('o', "Output", HelpText = "The folder where the files will be stored.")]
public string OutputFolder { get; set; }
}
}
42 changes: 41 additions & 1 deletion AudioBookSplitterCmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CommandLine.Text;

namespace AudioBookSplitterCmd
{
internal class Program
{
private static void Main(string[] args)
{
var parser = new Parser(config => config.HelpWriter = Console.Out);
var parser = new Parser(config => config.HelpWriter = null);
var result = parser.ParseArguments<Options>(args);
result.WithNotParsed(errors =>
{
foreach (var error in errors)
{
if (error.Tag != ErrorType.HelpRequestedError &&
error.Tag != ErrorType.VersionRequestedError) continue;
Console.WriteLine(BuildHelp(result));
Environment.Exit(0);
}
var myHelpText = HelpText.AutoBuild(result, onError => BuildHelp(result), onExample => onExample);
Console.Error.WriteLine(myHelpText);
Environment.Exit(1);
});

if (result.GetType() != typeof(Parsed<Options>))
{
Expand Down Expand Up @@ -114,6 +132,28 @@ private static void Main(string[] args)
Console.ReadKey(true);
}

private static HelpText BuildHelp(ParserResult<Options> result)
{
var assembly = typeof(Program).GetTypeInfo().Assembly;

var assemblyTitleAttribute = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute)).SingleOrDefault() as AssemblyTitleAttribute;
var assemblyCopyrightAttribute = assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute)).SingleOrDefault() as AssemblyCopyrightAttribute;
var assemblyCompanyAttribute = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute)).SingleOrDefault() as AssemblyCompanyAttribute;
var assemblyDescriptionAttribute = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute)).SingleOrDefault() as AssemblyDescriptionAttribute;
var version = assembly.GetName().Version.ToString().ToString(CultureInfo.InvariantCulture);

var nHelpText = new HelpText(SentenceBuilder.Create(), $"{assemblyTitleAttribute?.Title} {version}"
+ $"{(assemblyCopyrightAttribute == null && assemblyCompanyAttribute == null ? "" : "\r\n" + (assemblyCopyrightAttribute?.Copyright))} {assemblyCompanyAttribute?.Company}"
+ $"{((assemblyDescriptionAttribute == null) ? "" : "\r\n" + assemblyDescriptionAttribute.Description)}")
{
AdditionalNewLineAfterOption = false,
AddDashesToOption = true,
MaximumDisplayWidth = 4000,
AddEnumValuesToHelpText = true
};
nHelpText.AddOptions(result);
return HelpText.DefaultParsingErrorsHandler(result, nHelpText);
}

}
}
2 changes: 1 addition & 1 deletion AudioBookSplitterCmd/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[assembly: AssemblyCompany("Prometeo")]
[assembly: AssemblyProduct("AudioBook Splitter")]
[assembly: AssemblyCopyright("Copyright © 2019 Prometeo")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]

[assembly: AssemblyVersion("1.1.81.7000")]
Expand Down
2 changes: 1 addition & 1 deletion AudioBookSplitterCmd/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="2.4.3" targetFramework="net461" />
<package id="CommandLineParser" version="2.5.0" targetFramework="net461" />
<package id="Humanizer.Core" version="2.5.16" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
</packages>
Loading

0 comments on commit 24c6504

Please sign in to comment.