public static CommandResult RemoveCommand(CommandLineProto proto, CommandLine args) { 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 defaultLen = proto.GetValue <int>(Names.LengthSwitch); int len = args.GetValue <int>(Names.LengthSwitch, Names.LengthMnemonic, defaultLen); if (pos < 1 || pos + len - 1 > folders.Count) { throw new ApplicationException("Position and length outside valid range for remove operation"); } if (!isQuiet) { app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString()); app.WriteError(); commit = app.GetConfirmation($"Remove {len} folder(s) from PATH starting at position {pos}?"); app.WriteError(); } if (commit) { int l = len; while (l-- > 0) { folders.RemoveAt(pos - 1); } } if (options == PathFolderOptions.Process) { app.WriteOut(folders.ToInlineString()); } else if (commit) { folders.Commit(); } if (commit) { return(new CommandResult(true, $"{len} folder(s) removed from PATH")); } else { return(new CommandResult(false, "Remove operation canceled")); } } catch (Exception ex) { return(new CommandResult(false, "Unable to remove folders from PATH", 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)); } }