Simplify command line argument parsing

This commit is contained in:
Hylke Bons 2012-06-15 13:04:56 +01:00
parent 07c4d271e3
commit 7ceb17907d
8 changed files with 31 additions and 1185 deletions

View file

@ -44,7 +44,6 @@
<Compile Include="SparkleAnnouncement.cs" />
<Compile Include="SparkleHelpers.cs" />
<Compile Include="SparklePaths.cs" />
<Compile Include="SparkleOptions.cs" />
<Compile Include="SparkleChangeSet.cs" />
<Compile Include="SparkleListenerBase.cs" />
<Compile Include="SparkleListenerFactory.cs" />

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
if [[ $UID -eq 0 ]]; then
echo "Cannot run as root. Things would go utterly wrong."
echo "SparkleShare can't be run as root. Things would go utterly wrong."
exit 1
fi
@ -18,7 +18,7 @@ start() {
echo "SparkleShare is already running."
exit 0
else
echo "SparkleShare stale pid file found, starting a new instance."
echo "Stale SparkleShare PID file found, starting a new instance..."
rm -f $pidfile
fi
fi
@ -42,7 +42,7 @@ stop() {
rm -f ${pidfile}
echo "Done."
else
echo "SparkleShare is not running, removing stale pid file..."
echo "SparkleShare is not running, removing stale PID file..."
rm -f ${pidfile}
fi
else
@ -65,13 +65,7 @@ case $1 in
invite=`date -u +%N`
curl -o ~/SparkleShare/$invite.xml `echo $2 | sed s/sparkleshare:/https:/`
;;
help|--help|-h)
*)
mono "@expanded_libdir@/@PACKAGE@/SparkleShare.exe" --help
;;
version|--version|-v)
mono "@expanded_libdir@/@PACKAGE@/SparkleShare.exe" --version
;;
*)
echo "Usage: sparkleshare {start|stop|restart|help|version}"
;;
esac

View file

@ -94,9 +94,6 @@
<Compile Include="..\SparklePlugin.cs">
<Link>SparklePlugin.cs</Link>
</Compile>
<Compile Include="..\SparkleOptions.cs">
<Link>SparkleOptions.cs</Link>
</Compile>
<Compile Include="SparkleAbout.cs" />
<Compile Include="..\SparkleInvite.cs">
<Link>SparkleInvite.cs</Link>

View file

@ -17,7 +17,6 @@ SOURCES = \
SparkleEventLogController.cs \
SparkleExtensions.cs \
SparkleInvite.cs \
SparkleOptions.cs \
SparklePlugin.cs \
SparkleSetupController.cs \
SparkleStatusIconController.cs

View file

@ -21,7 +21,6 @@ using System.Threading;
#if __MonoCS__
using Mono.Unix;
#endif
using SparkleLib;
namespace SparkleShare {
@ -30,18 +29,18 @@ namespace SparkleShare {
public static SparkleController Controller;
public static SparkleUI UI;
public static Mutex ProgramMutex = new Mutex (false, "SparkleShare");
private static Mutex program_mutex = new Mutex (false, "SparkleShare");
// Short alias for the translations
public static string _ (string s)
{
#if __MonoCS__
#if __MonoCS__
return Catalog.GetString (s);
#else
#else
return Strings.T (s);
#endif
#endif
}
@ -50,33 +49,30 @@ namespace SparkleShare {
#endif
public static void Main (string [] args)
{
// Parse the command line options
bool show_help = false;
OptionSet option_set = new OptionSet () {
{ "v|version", _("Print version information"), v => { PrintVersion (); } },
{ "h|help", _("Show this help text"), v => show_help = v != null }
};
if (args.Length != 0 && !args [0].Equals ("start")) {
Console.WriteLine (" ");
Console.WriteLine ("SparkleShare is a collaboration and sharing tool that is ");
Console.WriteLine ("designed to keep things simple and to stay out of your way.");
Console.WriteLine (" ");
Console.WriteLine ("Version: " + SparkleLib.Defines.VERSION);
Console.WriteLine ("Copyright (C) 2010 Hylke Bons");
Console.WriteLine (" ");
Console.WriteLine ("This program comes with ABSOLUTELY NO WARRANTY.");
Console.WriteLine (" ");
Console.WriteLine ("This is free software, and you are welcome to redistribute it ");
Console.WriteLine ("under certain conditions. Please read the GNU GPLv3 for details.");
Console.WriteLine (" ");
Console.WriteLine ("Usage: sparkleshare [start|stop|restart]");
try {
option_set.Parse (args);
} catch (OptionException e) {
Console.Write ("SparkleShare: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `sparkleshare --help' for more information.");
Environment.Exit (-1);
}
if (show_help)
ShowHelp (option_set);
// Only allow one instance of SparkleShare
if (!ProgramMutex.WaitOne (0, false)) {
// Only allow one instance of SparkleShare (on Windows)
if (!program_mutex.WaitOne (0, false)) {
Console.WriteLine ("SparkleShare is already running.");
Environment.Exit (-1);
}
// Initialize the controller this way so that
// there aren't any exceptions in the OS specific UI's
Controller = new SparkleController ();
@ -93,37 +89,5 @@ namespace SparkleShare {
GC.WaitForPendingFinalizers ();
#endif
}
// Prints the help output
public static void ShowHelp (OptionSet option_set)
{
Console.WriteLine (" ");
Console.WriteLine (_("SparkleShare, a collaboration and sharing tool."));
Console.WriteLine (_("Copyright (C) 2010 Hylke Bons"));
Console.WriteLine (" ");
Console.WriteLine (_("This program comes with ABSOLUTELY NO WARRANTY."));
Console.WriteLine (" ");
Console.WriteLine (_("This is free software, and you are welcome to redistribute it "));
Console.WriteLine (_("under certain conditions. Please read the GNU GPLv3 for details."));
Console.WriteLine (" ");
Console.WriteLine (_("SparkleShare is a collaboration and sharing tool that is "));
Console.WriteLine (_("designed to keep things simple and to stay out of your way."));
Console.WriteLine (" ");
Console.WriteLine (_("Usage: sparkleshare [start|stop|restart|version] [OPTION]..."));
Console.WriteLine (_("Sync SparkleShare folder with remote repositories."));
Console.WriteLine (" ");
Console.WriteLine (_("Arguments:"));
option_set.WriteOptionDescriptions (Console.Out);
Environment.Exit (0);
}
public static void PrintVersion ()
{
Console.WriteLine (_("SparkleShare " + Defines.VERSION));
Environment.Exit (0);
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -263,8 +263,7 @@ namespace SparkleShare {
full_name = full_name.Trim ();
email = email.Trim ();
bool fields_valid = (full_name != null &&
full_name.Trim ().Length > 0 &&
bool fields_valid = (!string.IsNullOrEmpty (full_name) &&
IsValidEmail (email));
if (UpdateSetupContinueButtonEvent != null)
@ -381,10 +380,8 @@ namespace SparkleShare {
this.saved_remote_path = remote_path;
bool fields_valid = (address != null &&
address.Trim ().Length > 0 &&
remote_path != null &&
remote_path.Trim ().Length > 0 &&
bool fields_valid = (!string.IsNullOrEmpty (address) &&
!string.IsNullOrEmpty (remote_path) &&
!remote_path.Contains ("\""));
if (UpdateAddProjectButtonEvent != null)

View file

@ -90,9 +90,6 @@
<Compile Include="..\SparkleInvite.cs">
<Link>SparkleInvite.cs</Link>
</Compile>
<Compile Include="..\SparkleOptions.cs">
<Link>SparkleOptions.cs</Link>
</Compile>
<Compile Include="..\SparklePlugin.cs">
<Link>SparklePlugin.cs</Link>
</Compile>
@ -451,4 +448,4 @@
<Content Include="..\..\data\icons\sparkleshare.ico" />
</ItemGroup>
<ItemGroup />
</Project>
</Project>