public static CommandResult AddCommand(CommandLineProto proto, CommandLine args) { var app = ConsoleApp.Instance; try { int defaultPos = proto.GetValue <int>(Names.PositionSwitch); int pos = args.GetValue <int>(Names.PositionSwitch, Names.PositionMnemonic, defaultPos); var options = GetPathFolderOptions(args); bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic); bool isQuiet = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic); var folders = new PathFolders(options); folders.Fill(); var added = new PathFolders(options); int parg = 2; string path = args.GetResolvedValue <string>(parg++); bool commit = true; while (path != null) { added.Add(new PathFolder(path)); path = args.GetResolvedValue <string>(parg++); } if (!isQuiet) { app.WriteError(); app.WriteError("Add..."); app.WriteError(); app.WriteError(added.ToString(), false); app.WriteError(); app.WriteError("to..."); app.WriteError(); app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString()); app.WriteError(); commit = app.GetConfirmation($"at position {pos}?"); app.WriteError(); } if (commit) { int p = pos - 1; foreach (var f in added) { folders.Insert(p++, f); } } if (options == PathFolderOptions.Process) { app.WriteOut(folders.ToInlineString()); } else if (commit) { folders.Commit(); } if (commit) { return(new CommandResult(true, $"{added.Count} folder(s) added to PATH at position {pos}")); } else { return(new CommandResult(false, "Add folders operation canceled")); } } catch (Exception ex) { return(new CommandResult(false, "Unable to add folders to command", ex)); } }
public static CommandResult MoveCommand(CommandLineProto proto, CommandLine args) { CommandResult result = new CommandResult(); try { var app = ConsoleApp.Instance; var options = GetPathFolderOptions(args); bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic); bool isQuiet = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic); var folders = new PathFolders(options); bool commit = true; folders.Fill(); int defaultPos = proto.GetValue <int>(Names.PositionSwitch); int pos = args.GetValue <int>(Names.PositionSwitch, Names.PositionMnemonic, defaultPos); int defaultLength = proto.GetValue <int>(Names.LengthSwitch); int len = args.GetValue <int>(Names.LengthSwitch, Names.LengthMnemonic, defaultLength); int defaultTo = proto.GetValue <int>(Names.ToSwitch); int to = args.GetValue <int>(Names.ToSwitch, Names.ToMnemonic, defaultTo); if (pos < 1 || pos + len - 1 > folders.Count) { throw new ApplicationException("Position and length outside valid range for move operation"); } if (to < 1 || to > folders.Count) { throw new ApplicationException("Move to position outside valid range"); } if (pos == to) { throw new ApplicationException("The source position and the destination position are the same"); } if (!isQuiet) { app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString()); app.WriteError(); commit = app.GetConfirmation($"Move {len} folder(s) in PATH starting at position {pos} to position {to}?"); app.WriteError(); } if (commit) { int l = len; int p = pos - 1; int t = to - 1; while (l-- > 0) { if (t < p) { folders.Insert(t, folders[p]); folders.RemoveAt(p + 1); ++p; ++t; } else { folders.Insert(t + len, folders[p]); folders.RemoveAt(p); } } } if (options == PathFolderOptions.Process) { app.WriteOut(folders.ToInlineString()); } else if (commit) { folders.Commit(); } if (commit) { return(new CommandResult(true, $"{len} folders moved from position {pos} to position {to} in PATH")); } else { return(new CommandResult(false, "Move operation canceled")); } } catch (Exception ex) { return(new CommandResult(false, "Unable to move folders in PATH", ex)); } }