我有点击装饰器的功能。该函数的全部目的是为tq_oex_interchange类正在完成的工作添加一个CLI选项。
using System;
using System.Collections.Generic;
public class Item
{
public static List<Item> Database;
static Item()
{
Database = new List<Item>();
}
public Item(string name, int start, int end, params string[] orders)
{
Name = name;
Start = start;
End = end;
Orders = new List<string>();
foreach (string s in orders)
Orders.Add(s);
//putting newly created Item to database
Database.Add(this);
}
//overload for creating tmp Items in GroupThem(), could be done using optinional parameter
public Item(bool AddToDatabase, string name, int start, int end, params string[] orders)
{
Name = name;
Start = start;
End = end;
Orders = new List<string>();
foreach (string s in orders)
Orders.Add(s);
if (AddToDatabase) Database.Add(this);
}
public string Name { get; set; }
public int Start { get; set; }
public int End { get; set; }
public List<string> Orders { get; set; }
public List<Item> GroupedItems()
{
List<Item> groupedItems = new List<Item>();
Item previous = Database[0];
Stack<Item> sameItems = new Stack<Item>();
foreach (Item item in Database)
{
if (previous.Name == item.Name)
{
sameItems.Push(item);
}
else
{
groupedItems.Add(GroupThem(sameItems));
previous = item;
sameItems.Push(item);
}
}
groupedItems.Add(GroupThem(sameItems));
return groupedItems;
}
private Item GroupThem(Stack<Item> sameItems)
{
string newName = "";
int newEnd = 0;
int newStart = int.MaxValue;
List<string> newOrders = new List<string>();
Item tmp = null;
while (sameItems.Count > 0)
{
tmp = sameItems.Pop();
if (tmp.Start < newStart)
newStart = tmp.Start;
if (tmp.End > newEnd)
newEnd = tmp.End;
foreach (string s in tmp.Orders)
if (!newOrders.Contains(s))
newOrders.Add(s);
newName = tmp.Name;
}
return new Item(false, newName, newStart, newEnd, newOrders.ToArray());
}
public override string ToString()
{
string tmp = "";
foreach (string s in Orders)
tmp += " " + s;
return "Name = " + Name + ", Start = " + Start + ", End = " + End +", Orders = "+ tmp;
}
}
class Program
{
static void Main(string[] args)
{
Item item1 = new Item("A", 1, 2, "C", "D");
Item item2 = new Item("A", 2, 3, "C", "E");
Item item3 = new Item("B", 4, 5, "F");
Item item4 = new Item("A", 6, 7);
Item item5 = new Item("A", 7, 8, "D");
Item item6 = new Item("A", 9, 10, "E");
foreach (Item item in item1.GroupedItems())
{
Console.WriteLine(item);
}
}
}
但是,当我使用命令
执行我的脚本时@click.command()
@click.option('-input_file', help='The input file to process')
@click.option('-output_file', help='The output file to create')
@click.option('-part_num_col', help='The column containing the part numbers')
@click.option('-summate_cols', help='The columns to summate')
def run(input_file, output_file, part_num_col, summate_cols):
from re import split
summate_cols = [int(i) for i in split(',', summate_cols)]
part_num_col = int(part_num_col)
teek = tq_oex_interchange()
click.echo("Working...")
teek.scan_file(input_file, output_file, part_num_col, summate_cols)
没有任何事情发生,甚至没有执行点击回声。
此外,
python tq_oex.py -input_file C:\Users\barnej78\Desktop\test_0.csv -output_file C:\Users\barnej78\Desktop\cmd.csv -part_num_col 3 -summate_cols 5,6
也没有做任何事情。
任何这些命令都没有错误或异常输出。
我做错了什么?
答案 0 :(得分:1)
您是否能够从此处成功运行示例代码?
我从那开始并确保它有效。然后使用Click test docs:
为它编写测试http://click.pocoo.org/5/testing/
这样当你开始调整它时,你可以运行测试来看看有什么破坏......
使用Click应用程序,我经常从一个参数开始简单,然后添加另一个以确保它仍然有效:
@click.command()
@click.argument('input_file')
def run_files(input_file):
click.echo(input_file)
然后为此添加一个选项:
@click.command()
@click.argument('input_file')
@click.option('--output_file', help='The output file to create')
def run_files(input_file, output_file):
click.echo(input_file, output_file)
我也喜欢设置默认值,以便进行调试:
def run_files(input_file='/path/to/input_file',
output_file='/path/to/output_file'):
click.echo(input_file, output_file)