import binascii
import os
import time

from optparse import OptionParser


def gen_random_object_id():
    timestamp = '{0:x}'.format(int(time.time()))
    rest = binascii.b2a_hex(os.urandom(8)).decode('ascii')
    return timestamp + rest

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-l', '--longform',
                      action="store_true",
                      dest="long_form",
                      help="prints the ID surrounded by ObjectId(...)")

    (options, args) = parser.parse_args()

    object_id = gen_random_object_id()

    if options.long_form:
        print('ObjectId("{}")'.format(object_id))
    else:
        print(object_id)

import binascii
import os
import time

from argparse import ArgumentParser


def gen_random_object_id():
    timestamp = '{0:x}'.format(int(time.time()))
    rest = binascii.b2a_hex(os.urandom(8)).decode('ascii')
    return timestamp + rest

if __name__ == '__main__':
    parser = ArgumentParser(description='Generate a random MongoDB ObjectId')
    parser.add_argument('-l', '--longform',
                        action="store_true",
                        dest="long_form",
                        help="prints the ID surrounded by ObjectId(...)")

    args = parser.parse_args()

    object_id = gen_random_object_id()

    if args.long_form:
        print('ObjectId("{}")'.format(object_id))
    else:
        print(object_id)

Use argparse instead of optparse
Use argparse instead of optparse

  import binascii
  import os
  import time
  
- from optparse import OptionParser
+ from argparse import ArgumentParser
  
  
  def gen_random_object_id():
      timestamp = '{0:x}'.format(int(time.time()))
      rest = binascii.b2a_hex(os.urandom(8)).decode('ascii')
      return timestamp + rest
  
  if __name__ == '__main__':
-     parser = OptionParser()
+     parser = ArgumentParser(description='Generate a random MongoDB ObjectId')
-     parser.add_option('-l', '--longform',
+     parser.add_argument('-l', '--longform',
-                       action="store_true",
+                         action="store_true",
-                       dest="long_form",
+                         dest="long_form",
-                       help="prints the ID surrounded by ObjectId(...)")
+                         help="prints the ID surrounded by ObjectId(...)")
  
-     (options, args) = parser.parse_args()
+     args = parser.parse_args()
  
      object_id = gen_random_object_id()
  
-     if options.long_form:
+     if args.long_form:
          print('ObjectId("{}")'.format(object_id))
      else:
          print(object_id)
  
Use argparse instead of optparse
## Code Before:
import binascii
import os
import time

from optparse import OptionParser


def gen_random_object_id():
    timestamp = '{0:x}'.format(int(time.time()))
    rest = binascii.b2a_hex(os.urandom(8)).decode('ascii')
    return timestamp + rest

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-l', '--longform',
                      action="store_true",
                      dest="long_form",
                      help="prints the ID surrounded by ObjectId(...)")

    (options, args) = parser.parse_args()

    object_id = gen_random_object_id()

    if options.long_form:
        print('ObjectId("{}")'.format(object_id))
    else:
        print(object_id)

## Instruction:
Use argparse instead of optparse
## Code After:
import binascii
import os
import time

from argparse import ArgumentParser


def gen_random_object_id():
    timestamp = '{0:x}'.format(int(time.time()))
    rest = binascii.b2a_hex(os.urandom(8)).decode('ascii')
    return timestamp + rest

if __name__ == '__main__':
    parser = ArgumentParser(description='Generate a random MongoDB ObjectId')
    parser.add_argument('-l', '--longform',
                        action="store_true",
                        dest="long_form",
                        help="prints the ID surrounded by ObjectId(...)")

    args = parser.parse_args()

    object_id = gen_random_object_id()

    if args.long_form:
        print('ObjectId("{}")'.format(object_id))
    else:
        print(object_id)
