如何在python中的类内使用argparse

时间:2018-08-21 03:11:20

标签: argparse

我正在尝试在类内部使用argparse。但是我很困惑,并且遇到了以下错误。该如何解决??

> bp_ts <- breakpoints(prices ~ 1)"
> summary(bp_ts)'

 Optimal (m+1)-segment partition: 

 Call:
 breakpoints.formula(formula = prices ~ 1)

 Breakpoints at observation number:

 m = 1   516                    
 m = 2   514 1016               
 m = 3   514 1014      1737     
 m = 4   514 1015      1882 2291
 m = 5   514 1014 1418 1884 2291

 Corresponding to breakdates:

 m = 1   0.191394658753709                                                      
 m = 2   0.190652818991098 0.376854599406528                                    
 m = 3   0.190652818991098 0.376112759643917                
 0.644287833827893
 m = 4   0.190652818991098 0.376483679525223                   
 0.698071216617211
 m = 5   0.190652818991098 0.376112759643917 0.525964391691395 
 0.698813056379822

 m = 1                    
 m = 2                    
 m = 3                    
 m = 4   0.849777448071217
 m = 5   0.849777448071217

 Fit:

 m   0         1         2         3         4         5        
 RSS 1.605e+10 4.759e+09 3.912e+09 3.872e+09 3.823e+09 3.820e+09
 BIC 4.972e+04 4.646e+04 4.595e+04 4.594e+04 4.592e+04 4.593e+04

 > breakdates(bp_ts, format.times = FALSE)'
 [1] 0.1906528 0.3764837 0.6980712 0.8497774

 > breakdates(bp_ts, format.times = TRUE)'
 [1] "0(515)"  "0(1016)" "0(1883)" "0(2292)"

1 个答案:

答案 0 :(得分:0)

我建议添加一些代码,以便您可以从控制台启动它:

 # This inside file add.py

if __name__ == "__main__":
    obj = add()
    parser = argparse.ArgumentParser(description='Pinging a server')
    parser.add_argument('-host','--Host',dest = 'Host',help='IP of server')
    parser.add_argument('-port','--Port',dest = 'Port',help='Port of server')
    parser.add_argument('-username','--User_name',des='User_name',help='User_name)
    parser.add_argument('-pwd','--Pwd',dest = 'Pwd',help='Pwd of server')
    parser.add_argument('-cmd','--Cmd',dest = 'Cmd',help='Cmd of server')

    args = parser.parse_args()

    obj.execute_remote_command(args.Host, args.Port, args.User_name, args.Pwd, args.Cmd)

然后您可以像这样启动它:

python add.py --Host your-host --Port your-port --User_name your-name --Pwd your-pwd --Cmd your-cmd

没有理由不起作用。 还可以考虑仅创建一个函数execute_remote_command。您没有在类中使用任何状态,这使其变得毫无用处。