Skip to content

Commit

Permalink
Printing help
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrenner committed Oct 14, 2016
1 parent 76af719 commit 853c4bf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 11 deletions.
6 changes: 2 additions & 4 deletions fget.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Release|Any CPU.Build.0 = Release|Any CPU
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Debug|Any CPU.ActiveCfg = Debug|x86
{DEB25DF4-20AF-4F85-8920-C3E14E147D9D}.Release|Any CPU.ActiveCfg = Release|x86
{9073E2EC-C147-421F-BAD6-4BFBD4275DC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9073E2EC-C147-421F-BAD6-4BFBD4275DC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9073E2EC-C147-421F-BAD6-4BFBD4275DC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
12 changes: 9 additions & 3 deletions src/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ internal Config Parse()

for (int i = 0; i < args.Length; i++)
{
if (IsTargetPath(i))
if (IsOption(i, "-o"))
{
ReadTargetPath(++i, config);
continue;
}

if (IsOption(i, "-h"))
{
config.Help = true;
continue;
}

config.SourceUrl = new Uri(args[i]);
}

Expand All @@ -37,9 +43,9 @@ private void ReadTargetPath(int index, Config config)
}
}

private bool IsTargetPath(int index)
private bool IsOption(int index, string option)
{
return string.Compare(args[index], "-o", true) == 0;
return string.Compare(args[index], option, true) == 0;
}
}
}
1 change: 1 addition & 0 deletions src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal class Config
{
public Uri SourceUrl;
public string TargetPath;
public bool Help;
}
}
3 changes: 2 additions & 1 deletion src/ConsoleDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ private void StartDownload(IFileDownloader fileDownloader)

private void DownloadProgressChanged(object sender, DownloadFileProgressChangedArgs eventArgs)
{
if (DateTime.Now - lastProgressUpdate < TimeSpan.FromSeconds(1)) return;
TimeSpan durationSinceLastUpdate = (DateTime.Now - lastProgressUpdate).Duration();
if (durationSinceLastUpdate < TimeSpan.FromSeconds(1)) return;

Console.CursorLeft = 0;
Console.Write("{0}%", eventArgs.ProgressPercentage);
Expand Down
19 changes: 19 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;

namespace FGet
{
Expand All @@ -11,6 +12,12 @@ public static int Main(string[] args)
var argumentParser = new ArgumentParser(args);
Config config = argumentParser.Parse();

if (config.Help)
{
ShowHelp();
return 0;
}

var consoleDownloader = new ConsoleDownloader(config);
if (!consoleDownloader.Download())
{
Expand All @@ -25,5 +32,17 @@ public static int Main(string[] args)
return 1;
}
}

private static void ShowHelp()
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine("FGet {0}.{1}", version.Major, version.Minor);
Console.WriteLine();
Console.WriteLine("Usage: fget [url] [option]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -h Print this help");
Console.WriteLine(" -o path Write file to the given path");
}
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

[assembly: InternalsVisibleTo("FGet.Tests")]
10 changes: 10 additions & 0 deletions tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public void NoArgumentShouldLeadToEmptyConfig()

Assert.True(config.SourceUrl == null);
Assert.True(config.TargetPath == null);
Assert.False(config.Help);
}

[Fact]
public void MinusHShouldLeadHelp()
{
var argumentParser = new ArgumentParser(new string[] { "-h" });
Config config = argumentParser.Parse();

Assert.True(config.Help);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Xunit;

namespace FGet.Tests
Expand All @@ -23,6 +24,13 @@ public void MissingUrlShouldReturnExitCode1()
int exitCode = Program.Main(new string [] {});
Assert.True(exitCode == 1);
}

[Fact]
public void MinusHShouldPrintHelp()
{
int exitCode = Program.Main(new string[] { "-h" });
Assert.True(exitCode == 0);
}
}
}
}

1 comment on commit 853c4bf

@stbrenner
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #1

Please sign in to comment.