
import argparse
import sys
from api import db
from api.tokens.models import Token
from sqlalchemy.exc import IntegrityError

def generate_token(name):
    token = Token(name)
    db.session.add(token)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
        sys.stderr.write('Failed to create token: Name {} already exists.\n'.format(name))
        sys.exit(-1)

    return token

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-n', '--name', type=str, required=True,
                        help='Name describing a sensor\'s location')
    args = parser.parse_args()

    token = generate_token(args.name)
    print('''
Successfully created token!

Name: {}
Token: {}

Dont forget to save this token in the sensor's configuration file.
'''.format(token.name, token.token.decode('utf-8')))

if __name__ == '__main__':
    main()


import argparse
import sys
from api import db
from api.tokens.models import Token
from sqlalchemy.exc import IntegrityError

def generate_token(name):
    token = Token(name)
    db.session.add(token)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
        sys.stderr.write('Failed to create token: Name {} already exists.\n'.format(name))
        sys.exit(-1)

    return token

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('name', type=str,
                        help='Name describing a sensor\'s location')
    args = parser.parse_args()

    token = generate_token(args.name)
    print('''
Successfully created token!

Name: {}
Token: {}

Dont forget to save this token in the sensor's configuration file.
'''.format(token.name, token.token.decode('utf-8')))

if __name__ == '__main__':
    main()

Change to positional argument for generate-token
Change to positional argument for generate-token

  
  import argparse
  import sys
  from api import db
  from api.tokens.models import Token
  from sqlalchemy.exc import IntegrityError
  
  def generate_token(name):
      token = Token(name)
      db.session.add(token)
      try:
          db.session.commit()
      except IntegrityError:
          db.session.rollback()
          sys.stderr.write('Failed to create token: Name {} already exists.\n'.format(name))
          sys.exit(-1)
  
      return token
  
  def main():
      parser = argparse.ArgumentParser(description=__doc__)
-     parser.add_argument('-n', '--name', type=str, required=True,
+     parser.add_argument('name', type=str,
                          help='Name describing a sensor\'s location')
      args = parser.parse_args()
  
      token = generate_token(args.name)
      print('''
  Successfully created token!
  
  Name: {}
  Token: {}
  
  Dont forget to save this token in the sensor's configuration file.
  '''.format(token.name, token.token.decode('utf-8')))
  
  if __name__ == '__main__':
      main()
  
Change to positional argument for generate-token
## Code Before:

import argparse
import sys
from api import db
from api.tokens.models import Token
from sqlalchemy.exc import IntegrityError

def generate_token(name):
    token = Token(name)
    db.session.add(token)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
        sys.stderr.write('Failed to create token: Name {} already exists.\n'.format(name))
        sys.exit(-1)

    return token

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-n', '--name', type=str, required=True,
                        help='Name describing a sensor\'s location')
    args = parser.parse_args()

    token = generate_token(args.name)
    print('''
Successfully created token!

Name: {}
Token: {}

Dont forget to save this token in the sensor's configuration file.
'''.format(token.name, token.token.decode('utf-8')))

if __name__ == '__main__':
    main()

## Instruction:
Change to positional argument for generate-token
## Code After:

import argparse
import sys
from api import db
from api.tokens.models import Token
from sqlalchemy.exc import IntegrityError

def generate_token(name):
    token = Token(name)
    db.session.add(token)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
        sys.stderr.write('Failed to create token: Name {} already exists.\n'.format(name))
        sys.exit(-1)

    return token

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('name', type=str,
                        help='Name describing a sensor\'s location')
    args = parser.parse_args()

    token = generate_token(args.name)
    print('''
Successfully created token!

Name: {}
Token: {}

Dont forget to save this token in the sensor's configuration file.
'''.format(token.name, token.token.decode('utf-8')))

if __name__ == '__main__':
    main()
