2015-07-21 65 views
-2

我沒有python的經驗。我正試圖弄清楚如何從命令行觸發這部分代碼。我發現的問題是,它看起來像「UserAccount()」是一個對象,我不知道如何從命令行觸發。所以a)什麼是UserAccount? b)我如何從命令行調用這個參數?從命令行調用python類

 # Create a random use account 
     randomInt = random.randint(1, 4294967295) 
     accountId = "random_id+" + str(randomInt) + "@acme.com" 
     randomInt = random.randint(1, 4294967295) 
     password = "Random_password1_" + str(randomInt) 
     primaryEmail = accountId 

     userAccount = UserAccount() 
     userAccount.accountId = accountId 
     userAccount.password = password 
     userAccount.primaryEmail = primaryEmail 
     userAccount.firstName = "Random" 
     userAccount.lastName = "User" 
     userAccount.birthdayMonth = 5 
     userAccount.birthdayDay = 31 

     #userAccount.firstNamePhonetic = "" 
     #userAccount.firstNameRomagi = "" 
     #userAccount.middleName = "" 
     #userAccount.middleNamePhonetic = "" 
     #userAccount.middleNameRomagi = "" 
     #userAccount.lastName = "" 
     #userAccount.lastNamePhonetic = "" 
     #userAccount.lastNameRomagi = "" 
     #userAccount.companyName = "" 
     #userAccount.securityQuestion = "" 
     #userAccount.securityAnswer = "" 
     #userAccount.locale = "" 
     #userAccount.timeZone = "" 
     #userAccount.allowUpdateNotification = False 
     #userAccount.allowThirdPartyNotification = False 

     # Assume for now that we're just creating a user account, not also requesting activate/deactivate 
     # permissions by providing a pre-created OAuth client ID. 
     oauthClientId = ""; 

     print(" Test creating an account: " + userAccount.accountId + ", password: " + userAccount.password + ", email: " + userAccount.primaryEmail) 
     accessToken = client.createAccount(userAccount, oauthClientId) 
     print(" Account created!") 
     if accessToken: 
      print("  Access token for account: " + accessToken) 
     print() 
+1

看看'argparse' – OMGtechy

+1

https://docs.python.org/3/library/__main__.html – aSteve

回答

1

答:UserAccount可能是一個類。 UserAccount()正在調用它的構造函數。請注意,該結構是通過呼叫而不是由new運營商完成的。 B:正如@OMGtechy已經說過的,看看argparse。下面的例子可能爲你工作:

import argparse 
parser = argparse.ArgumentParser(description='UserAccount creator') 

parser.add_argument('--id', '-i', required=True, help='Required. User Account Id') 
parser.add_argument('--password', '-p', required=True, help="Required. User Account's Password.") 
parser.add_argument('--email', '-e', required=True, help='Required. User Account E-Mail') 
parser.add_argument('--firstname', '-f', required=True, help="Required. User Account's first name") 
parser.add_argument('--lastname', '-l', required=True, help="Required. User Account's last name") 
parser.add_argument('--birthdaymonth', '-m', required=True, help="Required. User Account's Birthday month") 
parser.add_argument('--birthdayday', '-d', required=True, help="Required. User Account's Birthday day") 

args = parser.parse_args() 
userAccount = UserAccount() 
userAccount.accountId = args.id 
userAccount.password = args.password 
userAccount.primaryEmail = args.email 
userAccount.firstName = args.firstname 
userAccount.lastName = args.lastname 
userAccount.birthdayMonth = args.birthdaymonth 
userAccount.birthdayDay = args.birthdayday 

# Now rest, like normal 

oauthClientId = ""; 

print(" Test creating an account: " + userAccount.accountId + ", password: " + userAccount.password + ", email: " + userAccount.primaryEmail) 
accessToken = client.createAccount(userAccount, oauthClientId) 
print(" Account created!") 
if accessToken: 
    print("  Access token for account: " + accessToken) 
print() 
+0

謝謝!這將有所幫助。 – kgrondell