static int Main(string[] args) { if (!Utils.CheckAdministrator()) { return(0); } return(Parser.Default.ParseArguments <ListOptions, DefaultOptions, InstallOptions, RegisterOptions, UninstallOptions, UnregisterOptions, MoveOptions, DuplicateOptions, RunOptions, DirOptions, ConfigEnvOptions, ConfigUidOptions, ConfigKernelCmdOptions, ConfigFlagOptions>(args).MapResult( (ListOptions opts) => { Console.WriteLine(string.Join("\n", Wsl.ListDistros().ToArray())); return 0; }, (DefaultOptions opts) => { if (opts.Name == null) { Console.WriteLine(Wsl.GetDefaultDistro()); } else { Wsl.SetDefaultDistro(opts.Name); } return 0; }, (InstallOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.InstallDistro(opts.Name, opts.TarFile, opts.TarRootDirectory, opts.TargetDirectory); return 0; }, (RegisterOptions opts) => { Wsl.RegisterDistro(opts.Name, opts.InstallationDirectory); return 0; }, (UninstallOptions opts) => { Wsl.UninstallDistro(opts.Name); return 0; }, (UnregisterOptions opts) => { Wsl.UnregisterDistro(opts.Name); return 0; }, (MoveOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.MoveDistro(opts.Name, opts.TargetDirectory); return 0; }, (DuplicateOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.DuplicateDistro(opts.OldName, opts.NewName, opts.TargetDirectory); return 0; }, (RunOptions opts) => { return (int)Wsl.LaunchDistro(opts.Name, opts.Command, !opts.NoCwd); }, (DirOptions opts) => { Console.WriteLine(Wsl.GetInstallationDirectory(opts.Name)); return 0; }, (ConfigEnvOptions opts) => { var envVars = Wsl.GetDefaultEnvironment(opts.Name).ToList(); if (opts.VariableToAdd != null) { if (!opts.VariableToAdd.Contains('=')) { Utils.Error($"Environment variable must contain \"=\": \"{opts.VariableToAdd}\"."); } envVars.Add(opts.VariableToAdd); Wsl.SetDefaultEnvironment(opts.Name, envVars.ToArray()); } else if (opts.VariableToRemove != null) { if (opts.VariableToRemove.Contains('=')) { Utils.Error($"Environment variable name should not contain \"=\": \"{opts.VariableToRemove}\"."); } var newEnvVars = envVars.Where(s => !s.StartsWith($"{opts.VariableToRemove}=")); if (envVars.Count == newEnvVars.Count()) { Utils.Error($"Environment variable not found: {opts.VariableToRemove}."); } Wsl.SetDefaultEnvironment(opts.Name, newEnvVars.ToArray()); } else { Console.Write(string.Join("\n", envVars)); } return 0; }, (ConfigUidOptions opts) => { if (opts.Uid == null) { Console.WriteLine(Wsl.GetDefaultUid(opts.Name)); } else { Wsl.SetDefaultUid(opts.Name, opts.Uid.Value); } return 0; }, (ConfigKernelCmdOptions opts) => { if (opts.Commandline == null) { Console.WriteLine(Wsl.GetKernelCommandLine(opts.Name)); } else { Wsl.SetKernelCommandLine(opts.Name, opts.Commandline); } return 0; }, (ConfigFlagOptions opts) => { switch (opts.FlagName) { case "WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableInterop) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableInterop, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.AppendNtPath) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.AppendNtPath, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableDriveMounting) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableDriveMounting, opts.Flag != 0); } return 0; default: Utils.Error("Flag name not found."); return 1; // Never reached. } }, errs => 1 )); }
static int Main(string[] args) { Utils.CheckCompatibility(); Utils.CheckAdministrator(); return(Parser.Default.ParseArguments <ListOptions, DefaultOptions, InstallOptions, RegisterOptions, UninstallOptions, UnregisterOptions, MoveOptions, DuplicateOptions, RunOptions, DirOptions, ConfigEnvOptions, ConfigUidOptions, ConfigKernelCmdOptions, ConfigFlagOptions>(args).MapResult( (ListOptions opts) => { Console.WriteLine(string.Join(Environment.NewLine, Wsl.ListDistros().ToArray())); return 0; }, (DefaultOptions opts) => { if (opts.Name == null) { Console.WriteLine(Wsl.GetDefaultDistro()); } else { Wsl.SetDefaultDistro(opts.Name); } return 0; }, (InstallOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.InstallDistro(opts.Name, opts.TarFile, opts.TarRootDirectory, opts.TargetDirectory); return 0; }, (RegisterOptions opts) => { Wsl.RegisterDistro(opts.Name, opts.InstallationDirectory); return 0; }, (UninstallOptions opts) => { Wsl.UninstallDistro(opts.Name); return 0; }, (UnregisterOptions opts) => { Wsl.UnregisterDistro(opts.Name); return 0; }, (MoveOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.MoveDistro(opts.Name, opts.TargetDirectory); return 0; }, (DuplicateOptions opts) => { Utils.CheckCaseInsensitive(); Wsl.DuplicateDistro(opts.OldName, opts.NewName, opts.TargetDirectory); return 0; }, (RunOptions opts) => { Utils.CheckWslApi(); return (int)Wsl.LaunchDistro(opts.Name, opts.Command, !opts.NoCwd); }, (DirOptions opts) => { Console.WriteLine(Wsl.GetInstallationDirectory(opts.Name)); return 0; }, (ConfigEnvOptions opts) => { string getVarName(string envVar) { var id = envVar.IndexOf('='); if (id <= 0) { Utils.Error($"Invalid environment variable: \"{envVar}\"."); } return envVar.Substring(0, id); } var envVars = Wsl.GetDefaultEnvironment(opts.Name).ToDictionary(getVarName); void checkVarName(string varName) { if (!envVars.ContainsKey(varName)) { Utils.Error($"Environment variable not found: \"{varName}\"."); } } if (opts.VariableToAdd != null || opts.VariableToRemove != null) { if (opts.VariableToAdd != null) { envVars[getVarName(opts.VariableToAdd)] = opts.VariableToAdd; } else { checkVarName(opts.VariableToRemove); envVars.Remove(opts.VariableToRemove); } Wsl.SetDefaultEnvironment(opts.Name, envVars.Values); } else if (opts.VariableToGet != null) { checkVarName(opts.VariableToGet); Console.WriteLine(envVars[opts.VariableToGet]); } else { Console.WriteLine(string.Join(Environment.NewLine, envVars.Values)); } return 0; }, (ConfigUidOptions opts) => { if (opts.Uid == null) { Console.WriteLine(Wsl.GetDefaultUid(opts.Name)); } else { Wsl.SetDefaultUid(opts.Name, opts.Uid.Value); } return 0; }, (ConfigKernelCmdOptions opts) => { if (opts.Commandline == null) { Console.WriteLine(Wsl.GetKernelCommandLine(opts.Name)); } else { Wsl.SetKernelCommandLine(opts.Name, opts.Commandline); } return 0; }, (ConfigFlagOptions opts) => { switch (opts.FlagName) { case "WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableInterop) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableInterop, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.AppendNtPath) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.AppendNtPath, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableDriveMounting) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableDriveMounting, opts.Flag != 0); } return 0; default: Utils.Error("Flag name not found."); return 1; // Never reached. } }, errs => 1 )); }
static int Main(string[] args) { return(Parser.Default.ParseArguments <ListOptions, DefaultOptions, InstallOptions, RegisterOptions, UninstallOptions, UnregisterOptions, MoveOptions, RunOptions, DirOptions, ConfigEnvOptions, ConfigUidOptions, ConfigKernelCmdOptions, ConfigFlagOptions>(args).MapResult( (ListOptions opts) => { Console.WriteLine(string.Join("\n", Wsl.ListDistros().ToArray())); return 0; }, (DefaultOptions opts) => { if (opts.Name == null) { Console.WriteLine(Wsl.GetDefaultDistro()); } else { Wsl.SetDefaultDistro(opts.Name); } return 0; }, (InstallOptions opts) => { Wsl.InstallDistro(opts.Name, opts.TarGzFile, opts.TargetDirectory); return 0; }, (RegisterOptions opts) => { Wsl.RegisterDistro(opts.Name, opts.InstallationDirectory); return 0; }, (UninstallOptions opts) => { Wsl.UninstallDistro(opts.Name); return 0; }, (UnregisterOptions opts) => { Wsl.UnregisterDistro(opts.Name); return 0; }, (MoveOptions opts) => { Wsl.MoveDistro(opts.Name, opts.TargetDirectory); return 0; }, (RunOptions opts) => { return (int)Wsl.LaunchDistro(opts.Name, opts.Command); }, (DirOptions opts) => { Console.WriteLine(Wsl.GetInstallationDirectory(opts.Name)); return 0; }, (ConfigEnvOptions opts) => { if (opts.EnvironmentVariables == null) { Console.WriteLine(string.Join("\n", Wsl.GetDefaultEnvironment(opts.Name))); } else { Wsl.SetDefaultEnvironment(opts.Name, opts.EnvironmentVariables); } return 0; }, (ConfigUidOptions opts) => { if (opts.Uid == null) { Console.WriteLine(Wsl.GetDefaultUid(opts.Name)); } else { Wsl.SetDefaultUid(opts.Name, opts.Uid.Value); } return 0; }, (ConfigKernelCmdOptions opts) => { if (opts.Commandline == null) { Console.WriteLine(Wsl.GetKernelCommandLine(opts.Name)); } else { Wsl.SetKernelCommandLine(opts.Name, opts.Commandline); } return 0; }, (ConfigFlagOptions opts) => { switch (opts.FlagName) { case "WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableInterop) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableInterop, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.AppendNtPath) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.AppendNtPath, opts.Flag != 0); } return 0; case "WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING": if (opts.Flag == null) { Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableDriveMounting) ? 1 : 0); } else { Wsl.SetFlag(opts.Name, DistroFlags.EnableDriveMounting, opts.Flag != 0); } return 0; default: Console.Error.WriteLine("Flag name not found."); return 1; } }, errs => 1 )); }